我的应用程序使用 UIKeyboardWillShow
和 UIKeyboardWillHide
通知监视键盘的变化以动画化 UI 的特定部分。
我遇到的问题是,当我使用应用程序切换器(在不需要键盘的状态下)从显示键盘的消息应用程序切换到我的应用程序时,它将触发 UIKeyboardWillShow
通知,然后是 UIKeyboardWillHide
通知,这导致我的 UI 稍微上下跳动。
有没有办法只为您自己的应用监听键盘通知?
ViewWillAppear
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
ViewWillDisappear
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillChangeFrameNotification, object: nil)
最佳答案
有代码吗?
您是在整个应用程序中还是仅在 viewcontroller
中观察键盘?
每当你不想听变化你可以removeObserver
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] removeObserver:yourClass name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:yourClass name:UIKeyboardWillHideNotification object:nil];
}
但是要小心,你需要正确使用NSNotificationCenter
。
关于ios - UIKeyboardWillShow 来自其他应用程序的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685800/