Ios UIKeyboard将显示来自其他应用程序的通知
我的应用程序使用Ios UIKeyboard将显示来自其他应用程序的通知,ios,swift,Ios,Swift,我的应用程序使用UIKeyboardWillShow和UIKeyboardWillHide通知来监控键盘的更改,从而为用户界面的特定部分设置动画 我遇到的问题是,当我从显示键盘的Messages应用程序切换到使用应用程序切换器的应用程序时(在不需要键盘的状态下),它将触发UI键盘将显示通知,然后UI键盘将隐藏通知,这将导致我的UI上下跳动一点 有没有办法只监听你自己应用程序的键盘通知 视图将出现 NSNotificationCenter.defaultCenter().addObserv
UIKeyboardWillShow
和UIKeyboardWillHide
通知来监控键盘的更改,从而为用户界面的特定部分设置动画
我遇到的问题是,当我从显示键盘的Messages应用程序切换到使用应用程序切换器的应用程序时(在不需要键盘的状态下),它将触发UI键盘将显示
通知,然后UI键盘将隐藏
通知,这将导致我的UI上下跳动一点
有没有办法只监听你自己应用程序的键盘通知
视图将出现
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)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillChangeFrameNotification, object: nil)
视图将消失
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)
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];
}
但是要小心,您需要正确使用通知中心。我今天遇到了这个问题。我最后做的是添加观察者来检测应用程序何时进入和离开后台。如果进入后台,则移除键盘观察者,否则添加键盘观察者。像这样的。。。我相信有更好的方法,但这对我来说很有效。希望这有帮助
override func viewDidLoad() {
super.viewDidLoad()
registerForPreviewing(with: self, sourceView: collectionView)
addApplicationStateObservers()
}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
addKeyboardObservers()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
removeKeyboardObservers()
}
deinit {
removeApplicationStateObservers()
removeKeyboardObservers()
}
func applicationIsActive() {
let delay = DispatchTime.now() + 0.1
DispatchQueue.main.asyncAfter(deadline: delay) {
self.addKeyboardObservers()
}
}
// Observers
// Application state observers were add to handle edge case scenario where if user is playing a video in expanded pip view on iPhone, if user switchs to another app with keyboard then switch back, the video will disppears. Our app removes and re-adds observer as user enter and exit the app.
func addApplicationStateObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(applicationIsActive), name: .UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(removeKeyboardObservers), name: .UIApplicationDidEnterBackground, object: nil)
}
func removeApplicationStateObservers() {
NotificationCenter.default.removeObserver(self, name: .UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIApplicationDidEnterBackground, object: nil)
}
func addKeyboardObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden), name: .UIKeyboardWillHide, object: nil)
}
func removeKeyboardObservers() {
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}
谢谢你的回复。我只在ViewController中监视它。我将观察员添加到ViewWillExample(上面添加的代码)中,现在已将remove observer添加到ViewWillEnglish中,但当您切换回时,仍然无法获取键盘显示触发器。您确定离开应用程序时将执行“ViewWillEnglishe”中的代码吗?尝试在
视图中提供NSLog
以进行监视。这与您的要求无关,但您可能需要记住视图将出现
并不一定意味着您的视图确实会出现(我认为这是一个恼人的错误,甚至是一个巨大的错误,但这是生活中的事实),而视图将消失
并不一定意味着您的视图将确实消失。因此,您最好将这些方法移动到viewDid
,这意味着它所说的内容