gpt4 book ai didi

ios - 根据键盘高度更新文本字段的约束

转载 作者:行者123 更新时间:2023-11-28 15:00:46 24 4
gpt4 key购买 nike

抱歉,如果这看起来不太好,但我是第一次尝试编程并且还在学习。这是我的代码:

        func textFieldDidBeginEditing(_ textField: UITextField) {
var heightOfKeyboard = CGFloat(0.0)
UIView.animate(withDuration: 0.5) {

func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
heightOfKeyboard = keyboardHeight
}

}
print(heightOfKeyboard)
self.heightConstraint.constant = heightOfKeyboard + CGFloat(50)
self.view.layoutIfNeeded()
}
}

在我的控制台输出中,我得到 heighOfKeyboard 仍然是 0.0,我认为这是因为方法 textFieldDidBeginEditing 在 keyboardWillShow 之前被触发。如何在触发 textFieldDidBeginEditing 之前让 heightOfKeyboard 获取 keyboardHeight 的值?

感谢您的回答!!

最佳答案

你可以把所有的东西都展示出来

func keyboardWillShow(_ notification: Notification) {

if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
print(keyboardHeight )
self.heightConstraint.constant = keyboardHeight + CGFloat(50)

UIView.animate(withDuration: 0.5) {

self.view.layoutIfNeeded()
}

}

}

关于ios - 根据键盘高度更新文本字段的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48952391/

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