當系統收到顯示鍵盤的請求時,就從屏幕的底部滑出鍵盤,並將它放在應用程序內容的上方。由於鍵盤位於內容的上面,所以有可能遮掩住用戶希望編輯的文本對象,只能盲操^_^
如何解決可以參考iPhone應用程序編程指南 http://www.apple.com.cn/developer/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TextandWeb/TextandWeb.html#//apple_ref/doc/uid/TP40007072-CH20-SW16
大體思路是:暫時調整一或多個視圖的尺寸和位置,從而使文本對象可見。管理帶有鍵盤的文本對象的最簡單方法是將它們嵌入到一個UIScrollView(或其子類,如UITableView)對象。當鍵盤被顯示出來時,需要做的只是調整滾動視圖的尺寸,並將目標文本對象滾動到合適的位置。爲此,在UIKeyboardDidShowNotification通告的處理代碼中需要進行如下操作:
1. 取得鍵盤的尺寸。
2. 將滾動視圖的高度減去鍵盤的高度。
3. 將目標文本框滾動到視圖中。
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.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];
}
inView:cell
permittedArrowDirections:direction
animated:YES];
}