我的代码没有显示任何错误,但是当我启动模拟器时,应用程序崩溃了。我似乎找不到问题,但是当我实现“ keyboardWillShow”时,它开始崩溃。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow(with:)),
name: UIResponder.keyboardWillShowNotification,
object: nil)
}
// Mark: Actions
@objc func keyboardWillShow(with notification: Notification) {
let key = "UIKeyboardFrameEndUserInfoKey"
guard let keyboardFrame = notification.userInfo?[key] as? NSValue else { return }
let keyboardHeight = keyboardFrame.cgRectValue.height
buttomConstraint.constant = keyboardHeight
UIView.animate(withDuration: 0.2) {
self.view.layoutIfNeeded()
}
}
答案 0 :(得分:0)
Swift 4.2中有一些更改。我不确定,但是请在您的“ keyboardWillShow”-func中尝试以下操作:
let frameEndUserInfoKey = UIKeyboardFrameEndUserInfoKey
if let kbFrame = notification.userInfo?[frameEndUserInfoKey] as? CGRect {
let keyboardHeight = kbFrame.size.height
buttomConstraint.constant = keyboardHeight
UIView.animate(withDuration: 0.2) {
self.view.layoutIfNeeded()
}
}
答案 1 :(得分:0)
显然我很愚蠢,以至于忘记更改我的“ reuseIdentifier”。 代码从一开始就很好!感谢您的所有快速解答和帮助:D