i have multiple textField
and textView
in scrollView
in my viewController. i handle keyboard show and hide with these codes:
i added these line of code in viewDidLoad
:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:UIResponder.keyboardWillHideNotification, object: nil)
and also these 2 function:
@objc func keyboardWillShow(notification:NSNotification){
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
let bottom = keyboardViewEndFrame.height - view.safeAreaInsets.bottom + 16
self.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: bottom , right: 0)
}
@objc func keyboardWillHide(notification:NSNotification){
self.scrollView.contentInset = UIEdgeInsets.zero
}
everything is ok when i start to edit a textField. but it does not work with textView
and has problem to scroll to active textView.
how can i fix it?