ios - UIKeyboardWillShow 来自其他应用程序的通知

标签 ios swift

我的应用程序使用 UIKeyboardWillShowUIKeyboardWillHide 通知监视键盘的变化以动画化 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/

相关文章:

ios - 从 UITextView 的第一行创建标题(Think Apple Notes 应用程序)Swift

ios - Swift WebView 不加载 url html5 游戏内容

arrays - 声明具有特定类型但任意维数的数组

iOS:SQLite 插入查询不工作

c++ - 链接列表示例 - 为什么使用它们?

按索引范围快速过滤数组

swift - 为什么 DispatchWorkItem 会通知 crash?

ios - 如何在 Swift 中使用 CMAltitudeHandler?

iphone - iOS 如何在使用 UIApplication openURL 启动浏览器后退出当前应用程序

ios - 裁剪 CGPathRef 或 UIBezierPath