博客> 关于键盘知识点的总结
关于键盘知识点的总结
2019-10-11 15:56 评论:0 阅读:772 feng_zi
ISO 键盘的关闭 键盘位置的变化 键盘

开发中我们总会经常用到关于键盘的关闭,打开 和位置的变化问题,接下来给大家具体谈谈这几种情况: 1: 关于键盘位置的变化 首先在viewDidLoad 添加观察者

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; }

// 方法的实现

  • (void)keyboardWillHide:(NSNotification *)not { [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height); } completion:nil]; }

// 这是关于键盘位置的变化判断

pragma mark - UITextFieldDelegate

  • (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if (textField == _phoneMailTextField || textField == _pwdTextField || textField == _confirmTextField) { [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.view.frame = CGRectMake(0, 64.0f, self.view.frame.size.width, self.view.frame.size.height); } completion:nil]; } else { [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.view.frame = CGRectMake(0, -40, self.view.frame.size.width, self.view.frame.size.height); } completion:nil]; }

// 最后很重要的一点不要忘记移除通知

  • (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }

2: 关于键盘的隐藏 [view endEditing:YES] ; 或者让子视图成为第一响应者

收藏
0
sina weixin mail 回到顶部