ios 避免弹出键盘挡住屏幕

IOS中多个UITextField的键盘处理


IOS开发中使用UITextField时常需要考虑的问题就是键盘的处理。有时候,弹出的键盘会将UITextField区域覆盖,影响用户输入。这个时候就要将视图上移。这个时候我们需要考虑两点:
1,修改视图坐标的时机;
2,上移的偏移是多大。
特酷吧根据自己实际操作的实现方法如下:
1, 获取正在编辑的UITextField的指针
定义一个全局的UITextField的指针
UITextField *tempTextFiled;
在UITextFieldDelegate代理方法 -(void)textFieldDidBeginEditing:(UITextField *)textField
修正tempTextFiled的值为当前正在编辑的UITextField的地址。
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    tempTextFiled = textField;
}
2, 配置键盘处理事件
在- (void)viewDidLoad中实现键盘监听:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
实现键盘显示和键盘隐藏方法
在键盘显示方法中获取键盘高度,并配置键盘视图位移【值得一提的是,该方法会在用户切换中英文输入法的时候也会执行,因此不必担心在切换到中文输入法时键盘有多出一部分的问题】。
折叠 C/C++ Code 复制内容到剪贴板
  1. - (void)keyboardWillShow:(NSNotification *)notification  
  2. {  
  3.     NSDictionary * info = [notification userInfo];  
  4.     NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];  
  5.     CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];  
  6.     double keyboardHeight=keyboardRect.size.height;//键盘的高度  
  7.          
  8.     NSLog(@"textField superview].frame.origin.y = %f",[tempTextFiled superview].frame.origin.y);  
  9.     NSLog(@"keyboardHeight = %f",keyboardHeight);  
  10.     if ( ([tempTextFiled superview].frame.origin.y + keyboardHeight + REGISTERTABLE_CELL_HEGHIT) >= ([[UIScreen mainScreen] bounds].size.height-44))  
  11.     {  
  12.         //此时,编辑框被键盘盖住,则对视图做对应的位移  
  13.         CGRect frame =  CGRectMake(0, 44, 320, [[UIScreen mainScreen] bounds].size.height-45);  
  14.         frame.origin.y -= [tempTextFiled superview].frame.origin.y + keyboardHeight + REGISTERTABLE_CELL_HEGHIT +20 - [[UIScreen mainScreen] bounds].size.height + 44;//偏移量=编辑框原点Y值+键盘高度+编辑框高度-屏幕高度  
  15.         registerTableView.frame=frame;  
  16.     }  
  17. }  
然后实现键盘隐藏的处理:
在UITextFieldDelegate代理方法
-(void)textFieldDidEndEditing:(UITextField *)textFieldView 或者
- (void)keyboardWillHide:(NSNotification *)notification
方法中实现视图复位,如下代码:
CGRect frame =  registerTableView.frame;
frame.origin.y  = 44;//修改视图的原点Y坐标即可。
registerTableView.frame=frame;
3, 移除监听
在-(void)viewDidDisappear:(BOOL)animated或者dealloc方法中移除监听
[[NSNotificationCenter defaultCenter]   removeObserver:self   name:UIKeyboardDidShowNotification   object:nil];
[[NSNotificationCenter defaultCenter]   removeObserver:self   name:UIKeyboardDidHideNotification     object:nil];
这样,无论我们的界面上有多少UITextField,只需要简单的几部就可以实现UITextField不被键盘盖住。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值