gpt4 book ai didi

ios - KeyboardWillShow 中设置的执行顺序错误

转载 作者:行者123 更新时间:2023-11-30 11:58:00 25 4
gpt4 key购买 nike

我的ViewController View 包含scrollView及其父 View 的边界,并且scrollView的底部有一个tableView作为它的 subview 。 TableView 高度约束设置为其内容大小高度,其上方有文本字段。因此,当用户触摸文本字段时,键盘就会出现。为了让textField可见,我想向上滚动scrollView,但通常没有足够的空间,所以我想增加tableView的高度。问题是它的行为就像首先滚动scrollView,然后滚动tableViewConstraint已设置,因此当没有空间时它永远不会滚动足够。我可以“ dispatch ”吗 tableViewConstraint.constant = keyboardHeight以某种方式,它将首先在 UI 上执行。

override func viewDidLoad() {
tableViewConstraint.constant = TableView.contentSize.height
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
...
}



@objc func keyboardWillShow(notification:NSNotification){

if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {

let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height

tableViewConstraint.constant = keyboardHeight

let boundsOfOffsetView = scrollView.bounds.offsetBy(dx: CGFloat(0), dy: keyboardHeight)
scrollView.scrollRectToVisible(boundsOfOffsetView, animated: true)
}
}

最佳答案

您可以在 animate(withDuration:animations:completion:) 中进行布局更改阻止并调用 layoutIfNeeded() 以强制在您选择的时间发生更改。我会尝试这样的开始:

// Within your if statement…

let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height

UIView.animate(withDuration: 1.0/3.0) {
tableViewConstraint.constant = keyboardHeight
scrollView.layoutIfNeeded()
}

let boundsOfOffsetView = scrollView.bounds.offsetBy(dx: CGFloat(0), dy: keyboardHeight)
scrollView.scrollRectToVisible(boundsOfOffsetView, animated: true)

关于ios - KeyboardWillShow 中设置的执行顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47581954/

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