鍵盤通知keyboard,獲取鍵盤高度進行操做

 

iOS博主造了個輪子,引入.h就能夠自動偏移view,防止界面被鍵盤遮擋

 

-------------------分割線-------------------

 

博客更新,使用新的方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

這個方法能夠檢測輸入面板切換時候的通知,更加有效

- (void)onKeyboardNotification:(NSNotification *)notification {
    CGRect keyboardFrame = ((NSValue *) notification.userInfo[UIKeyboardFrameEndUserInfoKey]).CGRectValue;git

...
  }app

-------------------分割線-------------------ide

 

鍵盤通知

- (void)viewWillAppear:(BOOL)animated{
    //設置狀態欄白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    [self.textField becomeFirstResponder];
    self.textField.text = @"" ;
    //註冊鍵盤將要彈出的提醒
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    //註冊鍵盤將要消失時的提醒
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //移除一切編輯狀態
    [self.view endEditing:YES];
    //移除註冊的鍵盤將要顯示的通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
//    //移除註冊的鍵盤將要隱藏的通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    _tableView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64);
}動畫

方法裏面拿到鍵盤高度

- (void)keyboardShow:(NSNotification *)notification {
    NSDictionary * info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    NSLog(@"%f__________", kbSize.height);
    _tableView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64 - kbSize.height);spa

//也可使用UIView動畫.net

}
- (void)keyboardHide:(NSNotification *)notification {
//    NSDictionary * info = [notification userInfo];
//    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//    NSLog(@"%f__________", kbSize.height);
    _tableView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64);
}server

相關文章
相關標籤/搜索