我有一个使用自动布局的按钮位于屏幕底部。我正在尝试使按钮调整位置始终位于键盘上方。到目前为止,我已经能够让按钮向上移动,但我无法让按钮的位置重置。
我完全以编程方式做所有事情,我根本不使用 Storyboard。没有导出的解决方案将不胜感激。
我的viewDidLoad
函数(只展示了按钮移动相关的逻辑)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardMovedUp(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardMovedDown(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
keyboardMovedUp
函数
@objc
func keyboardMovedUp(notification: Notification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let viewEndFrame = view.convert(endFrame!, from: view.window)
let endFrameY = endFrame?.origin.y ?? 0
let animationRate = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue
let animationCurveRawNSM = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSM?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve: UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
self.moveUp = self.continueButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -viewEndFrame.origin.y)
self.moveUp?.isActive = true
UIView.animate(withDuration: animationRate!, delay: TimeInterval(0), options: animationCurve, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
和我的keyboardMovedDown
函数
@objc
func keyboardMovedDown(notification: Notification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
self.moveUp?.isActive = false
self.moveDown = self.continueButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20)
self.moveDown?.isActive = true
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
}
使用此代码,当键盘显示时,按钮将向上移动,但一旦键盘被重置,按钮不会移动位置。
我能够通过修改按钮的 origin.y
参数使代码正常工作,但我听说这会导致与自动布局的兼容性问题。
编辑:@IchBinStudenten 这是我当前代码经过您的更改后的样子
func viewDidLoad() {
self.moveUp = self.continueButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20)
self.moveUp?.isActive = true
}
@objc
func keyboardMoved(notification: Notification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let viewEndFrame = view.convert(endFrame!, from: view.window)
let endFrameY = endFrame?.origin.y ?? 0
let animationRate = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue
let animationCurveRawNSM = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSM?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve: UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
self.moveUp?.constant = -viewEndFrame.height - 20
UIView.animate(withDuration: animationRate!, delay: TimeInterval(0), options: animationCurve, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
@objc
func keyboardMovedDown(notification: Notification) {
if let userInfo = notification.userInfo {
self.moveUp?.constant = 0
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
}
最佳答案
使用1个约束就足以实现你想要的。无论如何,您正在使用一个约束。设置 self.moveUp
在键盘向上移动时激活,并改变它的值
self.moveUp?.constant = -20.0
当键盘向下移动时。为了使代码更简洁,只需在类中创建一些变量来保留对 self.continueButton.bottomAnchor.constraint(equalTo:self.view.bottomAnchor)
的引用,这样您就可以修改它的 <仅限 em>常量 属性。例如:
let keyboardConstraint: NSLayoutConstraint
override func viewDidLoad() {
self.keyboardConstraint = self.continueButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
self.keyboardConstraint.isActive = true
}
@objc
func keyboardMovedUp(notification: Notification) {
/// ...your code
self.keyboardConstraint.constant = -viewEndFrame.origin.y
UIView.animate(withDuration: animationRate!, delay: TimeInterval(0), options: animationCurve, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
@objc
func keyboardMovedDown(notification: Notification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
self.keyboardConstraint.constant = -20.0
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
}
}
另一种选择是安装 IQKeyboardManager通过 CocoaPods 为您管理这一切。
如果这能解决您的问题,请告诉我。
关于ios - 调整约束高度后,Swift 约束无法移回屏幕底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480492/