美文网首页iOS 开发每天分享优质文章
【Objective-c】 键盘遮挡问题解决

【Objective-c】 键盘遮挡问题解决

作者: MR_詹 | 来源:发表于2016-10-18 16:34 被阅读895次

项目开发中,常常会使用UITextView和UITextField输入内容,就会遇到输入框被键盘遮挡,致使用户看不到已输入的内容,体验不好。解决这个问题的方式有两种,
一种是在键盘的上方添加一个文本框,可以实时看到已输入的内容,输入完毕时再将内容赋值到对应的输入框就可以。
另一种也是今天小编要跟大家讲的:将这个视图往上移动,输入完毕再复原。


整体过程如下:
1、监听键盘获取键盘高度
2、计算输入框(UITextView和UITextField)的需调整的位移量
3、恢复视图原来的位置

@property (nonatomic, assign) CGFloat keyBoardHeight;   //键盘高度
@property (nonatomic, assign) CGRect originalFrame;    //记录视图的初始坐标
@property (nonatomic,strong) UITextView *currentTextView;   //当前输入框
1、监听键盘
#pragma-mark KeyboardNotification
-(void)viewWillAppear:(BOOL)animated{
    [self registerForKeyboardNotifications];
    [super viewWillAppear:animated];
}
//取消注册(必须)
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewWillDisappear:animated];
}
//注册
- (void)registerForKeyboardNotifications{
    //使用NSNotificationCenter 鍵盤出現時
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //键盘消失时
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
2、通过textView和textField的代理方法获取输入框对象,并记录视图初始位置frame,这里以textView为例子
#pragma-mark textFieldDelegate
-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.currentTextView = textView;
    self.originalFrame = self.view.frame;
}
3、计算视图self.view需要移动的偏移量
- (void)keyboardWillShow:(NSNotification*)notification{
    NSDictionary* info = [notification userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    _keyBoardHeight = kbSize.height;

    //第一步:将textField的坐标转换为相对整个屏幕的坐标值
    //坐标的转换可以参考此文章:http://www.jianshu.com/p/984049f1107c
    CGFloat controlY = [textView convertRect:currentTextView.bounds toView:[UIApplication sharedApplication].keyWindow].origin.y;
    //第二步:计算偏移值公式:(10是为了效果加上去的,可以自定义)
    //(屏幕高度 - 键盘高度)- (输入框的新坐标值Y + 输入框高度 + 10)
    CGFloat controlHeight = currentTextView.bounds.size.height;
    CGFloat offset = (SCREEN_H - _keyBoardHeight) - (controlY + controlHeight+10);
    CGFloat offsetY;
    //第三步:判断
    //如果输入框不会被键盘遮挡(offset>0)就保存原来的坐标就好
    if (offset>0) {
        offsetY = self.view.frame.origin.y;
    }else{
        offsetY = offset;
    }
    //设置视图移动的位移
    self.view.frame = CGRectMake(self.view.frame.origin.x,offsetY, self.view.frame.size.width, self.view.frame.size.height);

}
    
4、键盘消失恢复视图位置
//键盘消失恢复视图位置
- (void)keyboardWillHide:(NSNotification *)notification{
    CGFloat duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
       self.view.frame = self.originalFrame;
    }];
}

到这里,键盘的遮挡问题已经解决了。但小编还有一个问题没有解决的,就是如果输入框是textView,并且textView 的高度变化了,怎么继续移动视图?如果你有方法请在下方留言,小编感激不尽

相关文章

网友评论

    本文标题: 【Objective-c】 键盘遮挡问题解决

    本文链接:https://www.haomeiwen.com/subject/gfltuttx.html