ios Object-C 监听键盘弹出和取消键盘
在iOS中,可以使用以下方法来监听键盘的弹出和取消:
- 注册键盘弹出通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
- 注册键盘取消通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
- 实现键盘弹出时的回调方法:
- (void)keyboardWillShow:(NSNotification *)notification {
// 获取键盘尺寸
CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 处理键盘弹出逻辑
}
- 实现键盘取消时的回调方法:
- (void)keyboardWillHide:(NSNotification *)notification {
// 处理键盘取消逻辑
}
记得在不需要监听键盘的时候,要记得取消注册通知:
[[NSNotificationCenter defaultCenter] removeObserver:self];
``
原文地址: https://www.cveoy.top/t/topic/ixZR 著作权归作者所有。请勿转载和采集!