У меня есть экран с scrollView и bottomContainerView с двумя текстовыми полями и двумя кнопками.И я хочу, чтобы scrollView прокрутить вниз, когда клавиатура была показанаВот почему я сначала устанавливаю scrollView.contentOffset, а затем пытаюсь сделать видимым фрейм ForgotButton (чтобы прокрутить весь вид вниз).И в iOS 11.4 это работает.Но в iOS 12 нет.
private func adjustInsetForKeyboardShow(_ show: Bool, notification: Notification) {
let userInfo = notification.userInfo ?? [:]
if let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let adjustmentHeight = (keyboardFrame.height + 24) * (show ? 1 : 0)
scrollView.contentInset.bottom = adjustmentHeight
scrollView.scrollIndicatorInsets.bottom = adjustmentHeight
// set offset
let top = bottomContainerView.frame.minY + forgetButton.frame.minY
let height = forgetButton.frame.height
scrollView.scrollRectToVisible(CGRect(x: 0, y: top, width: 1, height: height), animated: true)
}
}
@objc
private func keyboardWillShow(_ notification: Notification) {
adjustInsetForKeyboardShow(true, notification: notification)
}
@objc
private func keyboardWillHide(_ notification: Notification) {
adjustInsetForKeyboardShow(false, notification: notification)
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
}
iOS 11.4
![iOS 11.4](https://i.stack.imgur.com/fzMTd.gif)
iOS 12.0
![enter image description here](https://i.stack.imgur.com/6ZpKs.gif)