我使用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)
}
}
然後在視圖控制器只是用:
func setUpView() {
okayButton.bindToKeyboard()
self.isHeroEnabled = true
}
但問題是,當我按下屏幕上的其他按鈕:輕敲其它按鈕後
保存按鈕消失,當它位於「上部位置」時,並且當它位於底部時出現。我究竟做錯了什麼?如何防止/修復它?
編輯:這些按鈕中沒有任何操作! (+, - ,保存)
謝謝!
鍵盤問題不隱藏。 –
這些按鈕中沒有任何操作。 – Magnifique
您是否嘗試將變換應用於視圖而非調整框架?我有一種感覺,這是與被調整的框架和佈局重新計算不正確有關。 – WsCandy