Salil Junior
Salil Junior

Reputation: 424

UITextview Keyboard handling when it is embedded in UIScrollView

So my iOS application has a UIScrollview set up in storyboard using autolayout with top, bottom, leading and trailing constraints bound to the superview. Inside the scrollview i have stackview with multiple UITextviews stacked vertically. The contentsize for scrollview is set fine and I can even scroll up and down.

Now, my issue comes up with keyboard handling. I am handling the keyboard same way recommended in apple's documentation: Apple's Keyboard handling documentation

The particular code that in question is shown below just for reference but it's pretty much just a copy-paste of Apple's example:

 func keyboardDidShow(notification: NSNotification){
    var info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets

    var aRect : CGRect = self.view.frame
    aRect.size.height -= keyboardSize!.height
    if self.activeTextView != nil {
        if (!aRect.contains(self.activeTextView!.bounds)){
            self.scrollView.scrollRectToVisible(self.activeTextView!.frame, animated: true)
        }
    }
}

My keyboard handling code works fine with UITextFields but it doesn't work with UITextFields. The self.scrollView.scrollRectToVisible(self.activeTextView!.frame, animated: true) gets called but no scrolling occurs.

I researched a bit and found out that Apple does not recommend embedding UITextView inside a uiscrollview: Placing a text view inside of a scroll view

Placing a text view inside of a scroll view. Text views handle their own scrolling. You should not embed text view objects in scroll views. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

However, I really need this "automatic" scrolling to hidden textview to work same way it does with UITextFields. Any suggestions??

PS: I should mention that I do not mind disabling scrolling in UITextViews as I already auto-adjust UITextViews to fit its contentsize using autolayout.

Upvotes: 1

Views: 1515

Answers (2)

Bikshapathi Kumbala
Bikshapathi Kumbala

Reputation: 64

I recommend you to use the following scrollview to handle the keyboard automatically.

https://github.com/michaeltyson/TPKeyboardAvoiding

Upvotes: 0

KevinM
KevinM

Reputation: 36

I would say although it’s not recommended to embed a UITextView in a UIScrollView, that should not prevent the scrollview from scrolling to a particular frame. Could you check if the frame you are attempting to scroll to (self.activeTextView.frame) is “correct”? Part of the unexpected UITextView-inside-scrollview behaviour could be that the textview’s frame isn’t what is expected. Sorry I am on the road and I won’t be able to try reproduce your issue and offer a more concrete solution but check the frame and if not what you would expect (given location of UITextView in the window) then perhaps try to convert rect from its parent view to that of your main view - if that is the issue then a workaround would be to embed your UITextView inside a UIView and scroll to UIView’s frame when keyboard appears..

Well try this code (although I have note tested it)…

  self.scrollView.scrollRectToVisible(self.activeTextView!.superview!.frame, animated: true)

Upvotes: 2

Related Questions