iPhone/iPad键盘弹出遮挡要编辑内…

来源:互联网 发布:淘宝上卖水果 编辑:程序博客网 时间:2024/06/05 10:25
 iPhone/iPad键盘弹出遮挡要编辑内容问题
2011-04-0216:17:31
标签:键盘 iPhone keyboard 遮盖 popover
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://afantihust.blog.51cto.com/2231549/533976
当系统收到显示键盘的请求时,就从屏幕的底部滑出键盘,并将它放在应用程序内容的上方。由于键盘位于内容的上面,所以有可能遮掩住用户希望编辑的文本对象,只能盲操^_^

如何解决可以参考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. 将目标文本框滚动到视图中。
 
但有时会碰到要编辑字段不在UIScrollView中的情况,比如烟草项目中点击靠近底部的烟草信息,弹出的popover可能会被弹出的键盘遮盖,这时通过简单的调整popover箭头方向即可实现弹出窗口随弹出键盘滑动的效果,当iPad竖着放置时点击列表中靠上部的行,箭头朝上;点击靠下部的行,箭头朝下;iPad横向放置时箭头朝右,效果如图所示:
 
iPad竖着放置时点击列表中靠上部的行,箭头朝上
 
 
点击靠下部的行,箭头朝下
 
竖向放置弹出键盘效果
 
横向放置时箭头朝右
 
横向放置弹出键盘效果

代码如下:
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(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 = [tableViewindexPathsForVisibleRows];
 BOOL upHalf = true;
 int halfIndex = indexArray.count / 4;
 if (indexPath.row > [[indexArrayobjectAtIndex:halfIndex] row]) {
  upHalf = false;
 }
 [self showOrderNumPopover:cellisUpHalf:upHalf];
 [orderNumViewController release];
}
-(void)showOrderNumPopover:(OrderOnlineCell *)cellisUpHalf:(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;
 }
 [orderNumPopoverpresentPopoverFromRect:popoverRect
         inView:cell
      permittedArrowDirections:direction
          animated:YES];
}

 

本文出自 “何必呢”博客,请务必保留此出处http://afantihust.blog.51cto.com/2231549/533976