當系統收到顯示鍵盤的請求時,就從屏幕的底部滑出鍵盤,並將它放在應用程序內容的上方。因爲鍵盤位於內容的上面,因此有可能遮掩住用戶但願編輯的文本對象,只能盲操^_^
大致思路是:暫時調整一或多個視圖的尺寸和位置,從而使文本對象可見。管理帶有鍵盤的文本對象的最簡單方法是將它們嵌入到一個UIScrollView(或其子類,如UITableView)對象。當鍵盤被顯示出來時,須要作的只是調整滾動視圖的尺寸,並將目標文本對象滾動到合適的位置。爲此,在UIKeyboardDidShowNotification通告的處理代碼中須要進行以下操做:
1. 取得鍵盤的尺寸。
2. 將滾動視圖的高度減去鍵盤的高度。
3. 將目標文本框滾動到視圖中。
但有時會碰到要編輯字段不在UIScrollView中的狀況,好比菸草項目中點擊靠近底部的菸草信息,彈出的popover可能會被彈出的鍵盤遮蓋,這時經過簡單的調整popover箭頭方向便可實現彈出窗口隨彈出鍵盤滑動的效果,當iPad豎着放置時點擊列表中靠上部的行,箭頭朝上;點擊靠下部的行,箭頭朝下;iPad橫向放置時箭頭朝右,效果如圖所示:
iPad豎着放置時點擊列表中靠上部的行,箭頭朝上
點擊靠下部的行,箭頭朝下
豎向放置彈出鍵盤效果
橫向放置時箭頭朝右
橫向放置彈出鍵盤效果
代碼以下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
OrderNumViewController *orderNumViewController = [[OrderNumViewController alloc] init];
orderNumViewController.containerViewController = self;
if (orderNumPopover == nil) {
orderNumPopover = [[UIPopoverController alloc] initWithContentViewController:orderNumViewController];
}else {
orderNumPopover.contentViewController = orderNumViewController;
}
OrderOnlineCell *cell = (OrderOnlineCell *)[tableView cellForRowAtIndexPath:indexPath];
NSArray *indexArray = [tableView indexPathsForVisibleRows];
BOOL upHalf = true;
int halfIndex = indexArray.count / 4;
if (indexPath.row > [[indexArray objectAtIndex:halfIndex] row]) {
upHalf = false;
}
[self showOrderNumPopover:cell isUpHalf:upHalf];
[orderNumViewController release];
}
-(void)showOrderNumPopover:(OrderOnlineCell *)cell isUpHalf:(BOOL)upHalf{
orderNumPopover.popoverContentSize = CGSizeMake(400, 320);
CGRect popoverRect = CGRectMake(cell.bounds.origin.x + cell.bounds.size.width - 100,
cell.bounds.origin.y,
27, 32);
UIInterfaceOrientation orientation = self.interfaceOrientation;
UIPopoverArrowDirection direction = UIPopoverArrowDirectionUnknown;
if ((orientation == UIInterfaceOrientationPortrait) || (orientation == UIInterfaceOrientationPortraitUpsideDown)) {
if (upHalf) {
direction = UIPopoverArrowDirectionUp;
}else {
direction = UIPopoverArrowDirectionDown;
}
}else {
direction = UIPopoverArrowDirectionRight;
}
[orderNumPopover presentPopoverFromRect:popoverRect inView:cell permittedArrowDirections:direction animated:YES]; }