iPhone/iPad键盘弹出遮挡要编辑内容问题

当系统收到显示键盘的请求时,就从屏幕的底部滑出键盘,并将它放在应用程序内容的上方。因为键盘位于内容的上面,因此有可能遮掩住用户但愿编辑的文本对象,只能盲操^_^
 
大致思路是:暂时调整一或多个视图的尺寸和位置,从而使文本对象可见。管理带有键盘的文本对象的最简单方法是将它们嵌入到一个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]; }
相关文章
相关标签/搜索