【发布时间】:2017-10-03 15:08:32
【问题描述】:
自从 Apple 推出 safe area insets 和 adjusted content insets 以来,已经工作的 UI 布局代码被破坏了。在我的情况下,当键盘出现时 UIScrollView 底部插图会扩展:
func keyboardWillResize(_ notification: Notification) {
let info: [AnyHashable: Any] = notification.userInfo!
let keyboardTop = self.view.frameOnScreen.maxY - (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.y
UIView.animate(withDuration: 0.3, animations: {
self.tableView.contentInset.bottom = keyboardTop
self.tableView.scrollIndicatorInsets = self.tableView.contentInset
})
}
在 iOS 11 中,此代码在键盘出现时产生额外的插入,等于标签栏的高度。这很明显,因为现在contentInset 只表示用户定义的 insets,而真正的 insets 由 iOS 11 中引入的adjustedContentInset 表示。
所以我的问题是如何以良好的方式处理这种情况?可以选择写
self.tableView.contentInset.bottom = keyboardTop - self.tableView.adjustedContentInset.bottom
但它看起来很丑。也许有内置的方法来扩展键盘后面的插图?
【问题讨论】:
标签: swift uiscrollview ios11