每当键盘出现时,我都会尝试为包含登录表单的UIStackView
设置动画。我只想通过调整storyboard文件中的约束来做到这一点。
这是我的布局:
我的代码会根据键盘的大小调整底部约束。
@IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
keyboardHeightLayoutConstraint.isActive = false
}
func keyboardNotification(notification: NSNotification) {
keyboardHeightLayoutConstraint.isActive = true
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
self.keyboardHeightLayoutConstraint?.constant = 0.0
} else {
self.keyboardHeightLayoutConstraint?.constant = (endFrame?.size.height)! + 20
}
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
结果是它只缩小了UIStackView的大小,因为顶部约束不允许它向上移动,例如:
这与我的stackview的顶级约束有关。删除此约束可以解决问题,但在较小的分辨率下,表单可能会与UIImageView
重叠。有没有办法更新此约束以反映底部约束的变化?
答案 0 :(得分:1)
您可以为顶部和底部约束设置出口并更改它们。我做了很多次。
我对你的约束布局并不完全清楚,但我猜你想要添加到底部布局的约束和减去< / strong>来自顶部布局约束。
答案 1 :(得分:0)
将该约束的优先级(从图像视图到堆栈视图的优先级)降低到200.优先级200低于默认内容压缩优先级,因此autolayout会在压缩其他视图之前将其中断。