快速约束在调整约束高度后无法移回屏幕底部



我有一个按钮,它使用自动布局坐在屏幕底部。我正在尝试使按钮调整位置以始终位于键盘上方。到目前为止,我已经能够让按钮向上移动,但我无法让按钮的位置重置。

我完全以编程方式完成所有操作,并且根本不使用故事板。没有插座的解决方案将不胜感激。

我的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)的引用,以便只能修改其constant属性。例如:

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()
}
}
}

另一种选择是通过CocoaPods安装IQKeyboardManager,它为您管理这一切。

让我知道这是否解决了您的问题。

为方便起见,请将约束设置为实例属性:

private lazy var continueButtonBottomAnchor = continueButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -bottomSafeArea)

将其添加为子视图时,在loadView中激活它:

continueButtonBottomAnchor.isActive = true

bottomSafeArea是一个计算属性,因为我假设您要将按钮放置在底部安全区域。计算属性不存储值(仅限计算(,因此可以在这些通知中使用正确的值延迟访问它们。

@objc private func keyboardMovedUp(_ notification: Notification) {
guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
return
}
continueButtonBottomAnchor.constant = -keyboardFrame.cgRectValue.height   
UIView.animate(withDuration: 0, animations: {
self.view.layoutIfNeeded()
})
}
@objc private func keyboardMovedDown() {
continueBUttonBottomAnchor.constant = -bottomSafeArea // using our computed property
UIView.animate(withDuration: 0, animations: {
self.view.layoutIfNeeded()
})
}

与接受的答案不同,不要添加自己的动画参数,也不要尝试获取动画参数(持续时间和时间(;通过在通知内进行动画处理,这些参数将自动应用,因此您的约束将与键盘完美同步。

最新更新