Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollView problem with UITextView when keyboard hide/show notification

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?

like image 673
Sajjad Avatar asked Sep 15 '25 08:09

Sajjad


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

like image 65
Rana Ijaz Avatar answered Sep 17 '25 03:09

Rana Ijaz