【发布时间】:2018-01-31 22:48:01
【问题描述】:
我正在使用 UIView Extension for button 来用键盘向上滑动它。
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
}
但问题是当我按下屏幕上的其他按钮时:
点击其他按钮后,保存按钮在“上部位置”时消失,在底部时出现。我究竟做错了什么?如何预防/修复它?
编辑:这些按钮中的任何一个都没有任何操作! (+,-,保存)
谢谢!
【问题讨论】:
-
键盘问题没有隐藏。
-
这些按钮没有任何动作。
-
您是否尝试过对视图应用变换而不是调整框架?我感觉这与调整框架和重新计算布局不正确有关。
-
显示“+”按钮操作代码和情节提要绑定快照。
标签: ios swift animation button