I've spent most of the day on SO trying to figure this out, here's my problem:
I have a UIScrollView
with a vertical stack view inside of it, and inside of the stack view I have several UIView
s that contain different stuff. In the bottom view, I have a UITextView
that I'd ideally like to move up whenever I start typing in it so I can actually see what I'm typing.
Implementing the solutions found here, here,here,here, and all the other similar prior questions on this topic works if I have a UITextField
in the last view instead of a UITextView
, but I really would like to have return key / multi-line functionality which seems to necessitate a UITextView
.
Here's what I have in terms of code (per the several above links):
func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil)
}
func keyboardWasShown(_ notification: NSNotification) {
guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
let keyboardFrame = keyboardFrameValue.cgRectValue
let keyboardSize = keyboardFrame.size
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
func keyboardWillBeHidden(_ notification: NSNotification) {
let contentInsets = UIEdgeInsets.zero
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
I've called registerForKeyboardNotifications()
in viewDidLoad()
, and this is working perfectly when I use a UITextField
, but for whatever reason the functionality doesn't work with a UITextView
. The closest SO question I could find to my issue is this one, but the question was never answered with a workable solution involving UITextView
. I've seen several posts mention IQKeyboardManager, but I'd ideally like to fix this natively.