keyboardWillShow 中的设置执行顺序错误
wrong order of execution of settings in keyboardWillShow
我的 ViewController 视图包含 scrollView 及其父视图的边界,并且在 scrollView 的底部有一个 tableView 作为它的子视图。 TableView 高度约束设置为其内容大小高度,并且在其上方有文本字段。所以当用户触摸 textField 键盘时出现。为了让 textField 可见,我想向上滚动 scrollView,但通常 space 不够,所以我想增加 tableView 的高度。问题是它的行为就像首先滚动 scrollView 然后设置 tableViewConstraint
所以当没有 space 时它永远不会滚动足够。我可以 '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)
我的 ViewController 视图包含 scrollView 及其父视图的边界,并且在 scrollView 的底部有一个 tableView 作为它的子视图。 TableView 高度约束设置为其内容大小高度,并且在其上方有文本字段。所以当用户触摸 textField 键盘时出现。为了让 textField 可见,我想向上滚动 scrollView,但通常 space 不够,所以我想增加 tableView 的高度。问题是它的行为就像首先滚动 scrollView 然后设置 tableViewConstraint
所以当没有 space 时它永远不会滚动足够。我可以 '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)