键盘通知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

相关文章
相关标签/搜索