處理輸入框被鍵盤遮蓋的問題

我們使用輸入框類的控件,有時候在屏幕底部會出現鍵盤遮蓋的問題。

有兩種方式處理: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


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