I have an extension which scrolls the scrollView up so it does not cover UITextField, however for UITextView it does not scroll up. The extension looks like this:
extension SummaryViewController {
///Register for keyboard willHide willShow notifiication
func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(SummaryViewController.keyboardNotification(notification:)),
name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration: TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve: UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
//close keyboard
scrollView.contentInset = .zero
} else {
//open keyboard
let height: CGFloat = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as? CGRect)!.size.height
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: height, right: 0)
}
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
}
I call registerKeyboardNotifications() in viewDidLoad.