scrollView problem with UITextView when keyboard hide/show notification

Viewed 508

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?

1 Answers

The reason of this issue is explained here, so if you want to use UITextView inside a UIScrollView then uncheck the Scrolling Enabled from right menu inspector or set it False from the code.

enter image description here

Related