gpt4 book ai didi

swift - 布局不会在 keyboardWillHide 方法上更新

转载 作者:行者123 更新时间:2023-11-28 10:40:12 25 4
gpt4 key购买 nike

我已经能够使用 keyboardWillShow 方法更新我的 scrollView 的布局约束。但是,当尝试在 keyboardWillHide 方法中执行相同操作时,布局不会更新(导致 scrollView 在键盘用于启动的位置被切断)。关于如何解决这个问题的任何提示?谢谢!!

@objc func keyboardWillShow(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
keyboardHeightSubtraction = keyboardFrame.size.height
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -keyboardHeightSubtraction).isActive = true
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}


@objc func keyboardWillHide(notification: NSNotification) {
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}

最佳答案

问题是您当前在约束中创建了冲突,因此在 viewDidLoad 中创建一次,并像这样在 2 个函数中管理它的常量\

var botCon:NSLayoutConstraint!

//

botCon = scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
botcon.isActive = true

//

@objc func keyboardWillShow(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
botCon.constant = -1 * keyboardHeightSubtraction
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}


@objc func keyboardWillHide(notification: NSNotification) {
botCon.constant = 0
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}

关于swift - 布局不会在 keyboardWillHide 方法上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50877792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com