36
votes

I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is,

  • I placed UIScrollView inside the main view in Storyboard
  • I placed UITextView inside UIScrollView in Storyboard and disabled Scrolling Enabled
  • I set constraints (leading, trailing, top, bottom) on UIScrollView
  • I set constraints (top, leading, trailing, height) on UITextView
  • I created IBOutlet of height constraint of UITextView
  • I set a text (a lot of text which can cause scrolling) on UITextView in viewDidLoad()
  • I set a height constraint of UITextView with the code below. I have tried it right after setting text in viewDidLoad() and viewDidLayoutSubviews() with no luck

self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height;

UITextView is getting its height, but UIScrollView isn't. Is there anything I've missed?

4
Another thing to note is that you may want UITextView.scrollEnabled = falseonmyway133
I have working example of how to calculate height for UITextView dynamic height + it's inside UIStackView which is inside UIScrollView here: github.com/fassko/ScrollStackViewExampleKristaps Grinbergs

4 Answers

87
votes

After a few days of research and getting my hands dirty with UIScrollView + UITextView + Auto Layout, I successfully got a fully working UIScrollView. I want to share my solution just in case someone might stuck on the same situation.

  1. Add UIScrollView inside the main view in Storyboard
  2. Add UIView inside the UIScrollView
  3. Add UITextView inside the UIView (the view added in step 2)
  4. Make sure "Scrolling Enabled" of UITextView is unchecked
  5. Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
  6. Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
  7. Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
  8. Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
  9. Add UITextView height constraint IBOutlet on the ViewController. @property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; and connect it in Storyboard
  10. Change the UITextView height constraint programmatically. self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;

After all of these 10 steps, you'll get fully working UIScrollView with UITextView inside and be happy.

8
votes

It seems that this question has an answer that has worked for a number of people. However, it should be noted that the documentation states:

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.

Before trying to put a UITextView in a UIScrollView you should consider if this is really necessary. If the text view is part of a complex layout within the scroll view, then note the use of the UIView container in the accepted answer.

See also: Scroll Views Inside Scroll Views

5
votes

If you noticed that the Auto Layout is still complaining about the height of the UIScrollView-

The problem here is that by default UITextView has checked the “Scrolling Enabled” in the IB.

So find that checkbox and uncheck it.

enter image description here

0
votes

Sodbileg's solution above works, but you do not need all of his steps. I got my scrollview to scroll with a text view inside by following his steps through step 7. For step 8, I added the 4 constraints but not the height (Don't forget the bottom constraint!!). I did not do the rest.

I am not sure, but it seems that the height of the textview adjusts automatically instead of us having to manually change it.