NSNotification.Name.UIKeyboardWillShow崩溃 – 无法找到原因
我的应用程序的用户报告了随机崩溃。 我已经集成了CrashAnalytics,它提供了以下详细信息:
__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20.
显示的行号为154,即:
self.notesView.content.frame = CGRect(x: self.notesView.content.frame.origin.x, y: self.notesView.content.frame.origin.y, width: self.notesView.content.frame.size.width, height: self.notesView.content.frame.size.height - keyboardFrame.size.height).
以下是我写的代码,包括这一行:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) label_title.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardShown), name:NSNotification.Name.UIKeyboardWillShow, object: nil); NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil); } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) label_title.removeObserver(self, forKeyPath: "contentSize") NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } func keyboardShown(notification: NSNotification) { let info = notification.userInfo! let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue self.notesView.content.frame = CGRect(x: self.notesView.content.frame.origin.x, y: self.notesView.content.frame.origin.y, width: self.notesView.content.frame.size.width, height: self.notesView.content.frame.size.height - keyboardFrame.size.height) }
首先,这是随机的,我从来没有得到它。 其次,我无法找到确切的原因。 这是因为通知观察者还是因为notesView(不是nil)。
正如这里建议的那样,我应该删除deinit中的键盘通知观察者吗?
如果有人以前经历过这个,请指导我。
将function的签名更改为此
@objc func keyboardShown(_ notification: Notification)
- 从UIViewController更改AppDelegate的属性不起作用
- HOWTO设置AddressBookAccessRequest alertView的文本?
- 来自UITableView中的节的条件目标视图控制器
- 在iOS上打开whatsapp,预先填写新的电话号码
- WatchOS2 WCSession WCSession可以在WatchKitExtension中拥有多少个代理?
- 将本机iOS应用连接到SQL Server Analysis Services多维数据集
- 多播委托 – 从iOS中的UI控件接收通知的多个类
- 根据UITableViewCell中的“UILabel”文本更改“UIView”的宽度
- MPRemoteCommandCenter不对MPMusicPlayerController执行任何操作