iOS开发-键盘

iOS键盘通知代码最佳实践

2018-08-01  本文已影响92人  Realank

每次用到键盘通知都从老代码里找,太麻烦了,总结一下:
首先在页面启动的时候加载通知

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

再在dealloc时移除通知

[[NSNotificationCenter defaultCenter] removeObserver:self];

键盘通知的接收方法:

#pragma mark - UIKeyboardNotification
- (void)keyboardWillShow:(NSNotification *)noti{
    CGFloat keyboardHeight = [[[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
}


- (void)keyboardWillHide:(NSNotification *)noti{
    CGFloat keyboardHeight = 0;
}

上一篇 下一篇

猜你喜欢

热点阅读