如何保持iOS上键盘出现时输入框不被覆盖
2012-04-17 19:21
441 查看
在iOS5上请求显示键盘时,系统从屏幕底部将键盘滑入上来,位于应用的内容之上。
如果屏幕中的内容项目比较多,它就可能覆盖住文本输入框之类的对象。你必须调整你的内容,使得输入框保持可见。
你会想到哪些处理方法呢?
第一种,
临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域空白。键盘的高度(keyboard.size.height)是一定的,将视图中所有内容所在区域的y值减小到y-keyboard.size.height。
该方法有个局限,如果所有内容之和大于窗口减去键盘高度的话,该方法将不能用。
第二种,
将窗口中所有视图嵌入进一个滚动视图对象(UIScrollView)中。在键盘出现时,你将输入框滚动到合适的位置,调整一下滚动视图的内容区域。
这些操作通过一个通知
1、根据通知的字典信息userInfo得到键盘的size。
2、根据键盘的size中的height值,调整滚动视图内容底部的inset。
3、滚动目标视图即文件输入框进入视图中。
简要的代码如下:
1、实现两个委托方法,用于指定输入框对象。
2、注册通知的观察者
将这个方法放在viewDidAppear中调用。
同时也要写一个removeObserver放在viewWillDisappear中调用。
3、实现键盘显示通知的selector中的方法
4、实现键盘消失通知的方法
这个方法调整内容底部的inset的值使得输入框不被键盘区域屏蔽的。还可以换种方法实现。
第三种,
扩展内容视图的高度,滚动文本输入框对象进内容视图。
将keyboardWasShown:重写。
如果屏幕中的内容项目比较多,它就可能覆盖住文本输入框之类的对象。你必须调整你的内容,使得输入框保持可见。
你会想到哪些处理方法呢?
第一种,
临时调整窗口中各个视图的大小,使得键盘从下向上占领的区域空白。键盘的高度(keyboard.size.height)是一定的,将视图中所有内容所在区域的y值减小到y-keyboard.size.height。
该方法有个局限,如果所有内容之和大于窗口减去键盘高度的话,该方法将不能用。
第二种,
将窗口中所有视图嵌入进一个滚动视图对象(UIScrollView)中。在键盘出现时,你将输入框滚动到合适的位置,调整一下滚动视图的内容区域。
这些操作通过一个通知
UIKeyboardDidShowNotification去实现的,逻辑过程如下:
1、根据通知的字典信息userInfo得到键盘的size。
2、根据键盘的size中的height值,调整滚动视图内容底部的inset。
3、滚动目标视图即文件输入框进入视图中。
简要的代码如下:
1、实现两个委托方法,用于指定输入框对象。
- (void)textFieldDidBeginEditing:(UITextField *)textField |
{ |
activeField = textField; |
} |
- (void)textFieldDidEndEditing:(UITextField *)textField |
{ |
activeField = nil; |
} |
- (void)registerForKeyboardNotifications |
{ |
[[NSNotificationCenter defaultCenter] addObserver:self |
selector:@selector(keyboardWasShown:) |
name:UIKeyboardDidShowNotification object:nil]; |
[[NSNotificationCenter defaultCenter] addObserver:self |
selector:@selector(keyboardWillBeHidden:) |
name:UIKeyboardWillHideNotification object:nil]; |
} |
同时也要写一个removeObserver放在viewWillDisappear中调用。
3、实现键盘显示通知的selector中的方法
// Called when the UIKeyboardDidShowNotification is sent. |
- (void)keyboardWasShown:(NSNotification*)aNotification |
{ |
NSDictionary* info = [aNotification userInfo]; |
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; |
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); |
scrollView.contentInset = contentInsets; |
scrollView.scrollIndicatorInsets = contentInsets; |
// If active text field is hidden by keyboard, scroll it so it's visible |
// Your application might not need or want this behavior. |
CGRect aRect = self.view.frame; |
aRect.size.height -= kbSize.height; |
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { |
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); |
[scrollView setContentOffset:scrollPoint animated:YES]; |
} |
} |
// Called when the UIKeyboardWillHideNotification is sent |
- (void)keyboardWillBeHidden:(NSNotification*)aNotification |
{ |
UIEdgeInsets contentInsets = UIEdgeInsetsZero; |
scrollView.contentInset = contentInsets; |
scrollView.scrollIndicatorInsets = contentInsets; |
} |
第三种,
扩展内容视图的高度,滚动文本输入框对象进内容视图。
将keyboardWasShown:重写。
- (void)keyboardWasShown:(NSNotification*)aNotification { |
NSDictionary* info = [aNotification userInfo]; |
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; |
CGRect bkgndRect = activeField.superview.frame; |
bkgndRect.size.height += kbSize.height; |
[activeField.superview setFrame:bkgndRect]; |
[scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES]; |
} 来源:http://www.th7.cn/Program/IOS/2012/04/16/70272.shtml |
相关文章推荐
- 如何保持iOS上键盘出现时输入框不被覆盖
- 如何保持iOS上键盘出现时输入框不被覆盖
- 如何保持iOS上键盘出现时输入框不被覆盖
- 如何保持iOS上键盘出现时输入框不被覆盖
- 保持iOS上键盘出现时输入框不被覆盖
- iOS上键盘出现时输入框不被覆盖的几种方法分析
- iOS上键盘出现时输入框不被覆盖的几种方法分析
- iOS上键盘出现时输入框不被覆盖的几种方法分析
- iOS上键盘出现时输入框不被覆盖的几种方法
- IOS开发 当键盘出现的时候,如何让UITextField自动上移
- 【实用】iOS开发iOS9中使用UIAlertView,弹窗消失时出现键盘闪现,导致输入框出现在屏幕中。
- 如何在键盘出现时滚动表格,以适应输入框的显示
- iOS 切换中文键盘时覆盖输入框
- 如何在键盘出现时滚动表格,以适应输入框的显示
- iOS键盘覆盖输入框的处理.doc
- IOS开发 ,当键盘出现的时候如何让UITextField自动上移
- iOS键盘覆盖输入框的处理.doc
- IOS-监听系统键盘及键盘出现时候输入框向上移动
- h5键盘遮挡输入框问题 、模仿微信输入框失去焦点时隐藏iphone的软键盘和聚焦时出现输入框
- 如何解决iOS模拟器调试时不能出现iPad/iPhone Simulator