ios – Swift:通过NSNotificationCenter的键盘观察器不起作用
发布时间:2020-12-14 18:57:44 所属栏目:百科 来源:网络整理
导读:我正在尝试在我的iOS 8 Swift应用程序中实现一个简单的键盘观察器,但它确实不起作用.这是我目前正在使用的代码: override func viewDidAppear(animated: Bool) { NSNotificationCenter().addObserver(self,selector: Selector(keyboardWillAppear()),name:
我正在尝试在我的iOS 8
Swift应用程序中实现一个简单的键盘观察器,但它确实不起作用.这是我目前正在使用的代码:
override func viewDidAppear(animated: Bool) { NSNotificationCenter().addObserver(self,selector: Selector(keyboardWillAppear()),name: UIKeyboardWillShowNotification,object: nil) NSNotificationCenter().addObserver(self,selector: Selector(keyboardWillHide()),name: UIKeyboardWillHideNotification,object: nil) } override func viewDidDisappear(animated: Bool) { NSNotificationCenter().removeObserver(self) } func keyboardWillAppear() { logoHeightConstraint.constant = 128.0 } func keyboardWillHide() { logoHeightConstraint.constant = 256.0 } 奇怪的是,在启动应用程序后,两个对键盘作出反应的功能都会被调用.当我进入或离开文本字段时没有任何反应.我究竟做错了什么?顺便说一下:改变约束是改变图像大小的最佳解决方案吗? 我真的很感谢你的帮助! 解决方法
每次调用NSNotificationCenter时,调用NSNotificationCenter()都会实例化一个新的NSNotificationCenter.请尝试使用NSNotificationCenter.defaultCenter().
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |