处理输入框被键盘遮盖的问题

我们使用输入框类的控件,有时候在屏幕底部会出现键盘遮盖的问题。

有两种方式处理:1.利用代理方法;2.利用监听键盘事件;

利用代理

意思就是在代理方法里面进行对父视图的Y抽偏移量的计算,上代码缺点是位移不够精确。
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"textFieldDidBeginEditing");
    CGRect frame = textField.frame;
    
    CGFloat heights = self.view.frame.size.height;
    
    // 当前点击textfield的座标的Y值 + 当前点击textFiled的高度 - (屏幕高度- 键盘高度 - 键盘上tabbar高度)
    
    // 在这一部 就是了一个 当前textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量
    
    int offset = frame.origin.y + 42- ( heights - 216.0-35.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;
    
    float height = self.view.frame.size.height;
    
    if(offset > 0) {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        
        self.view.frame = rect;
    }
    
    [UIView commitAnimations];

}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"textFieldDidEndEditing");
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    
    self.view.frame = rect;
    
    [UIView commitAnimations];
}

利用监听键盘事件

推荐大家利用这种方式:
- (void)addNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
}

#pragma mark - Notification
- (void)keyboardWillShow:(NSNotification *)notification {
    
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    //父view的偏移量 = 选中textfield的Y + 选中textfield的Height - (父view的height - keyboar的height - keyboard的tabbar的height)
    
    CGFloat offset = (CGRectGetMaxY(self.editTextField.frame)+CGRectGetHeight(self.editTextField.frame))-(CGRectGetHeight(self.view.frame)-CGRectGetHeight(keyboardRect));
    
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    if (offset > 0) {
        
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0.0f, -offset, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
        }];
        
    }
}
- (void)keyboardWillHide:(NSNotification *)notification {
    
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    }];
}
//获取当前正在编辑的textfield
- (void)textFieldBeginEditing:(NSNotification *)notification
{
    self.editTextField = [notification object];
}

总结

其实方法都是一样,最重要的是大家要理解重点,

view的Y抽偏移量 = 选中textfieldY + 选中textfieldHeight - (父viewheight - keyboarheight - keyboardtabbarheight


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章