我正在使用UIView Extension for按钮用键盘向上滑动。
extension UIView {
func bindToKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification) {
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let startingFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endingFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endingFrame.origin.y - startingFrame.origin.y
let options = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animate(withDuration: duration, delay: 0, options: options, animations: {
self.frame.origin.y += deltaY
self.layoutIfNeeded()
}, completion: nil)
}
}
然后在ViewController中使用:
func setUpView() {
okayButton.bindToKeyboard()
self.isHeroEnabled = true
}
但问题是当我按下屏幕上的其他按钮时:
点击其他按钮,当它处于“上部位置”时,保存按钮消失,当它在底部时出现。我究竟做错了什么?如何预防/修复它?
编辑:这些按钮上没有任何操作! (+, - ,保存)
谢谢!
答案 0 :(得分:2)
您不一定需要更新self.view
。您可以做的是为保存按钮创建IBOutlet
底部间距。
@IBOutlet weak var saveButtonBottomSpacing: NSLayoutConstraint!
您可以在UIView
动画块中进行此更改。
答案 1 :(得分:0)
在“保存”按钮成功操作后隐藏(辞退)键盘。
以下是您需要在“保存”按钮操作中更新的示例代码。
@IBAction func btnSave(sender: Any){
// add this line in your upon, successful action on save button
yourTextView.resignFirstResponder()
}