您的位置:首页 > 其它

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

2011-04-02 16:17 453 查看
当系统收到显示键盘的请求时,就从屏幕的底部滑出键盘,并将它放在应用程序内容的上方。由于键盘位于内容的上面,所以有可能遮掩住用户希望编辑的文本对象,只能盲操^_^

如何解决可以参考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横向放置时箭头朝右,效果如图所示:



[align=center]iPad竖着放置时点击列表中靠上部的行,箭头朝上[/align]
[align=center] [/align]



[align=center]点击靠下部的行,箭头朝下[/align]



[align=center]竖向放置弹出键盘效果[/align]
[align=center] [/align]



[align=center]横向放置时箭头朝右[/align]



[align=center]横向放置弹出键盘效果[/align]

代码如下:
- (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];
}

本文出自 “何必呢” 博客,请务必保留此出处http://afantihust.blog.51cto.com/2231549/533976
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: