James James - 1 year ago 121
iOS Question

UITextView not visible on UIScrollView

Ok, so what I have is a UIScrollView that is constrained to all four sides of the main view, centered both vertically and horizontally, and set to have equal width and height to the view. All of the subviews that I put on top of the UIScrollView are showing up when I run the app, exactly where I want them to be, but only the UITextView at the bottom is not. It seems like I've tried every combination of constraints but it never appears when I run the app regardless of what I do. Here is a screenshot of the constraints in the interface builder:

Interface Builder

And even when I preview the file Main.storyboard before running it looks like this:

Preview of Main.storyboard

But when I actually run the app, the screen is missing the UITextView, even when I alter the constraints in a number of ways:

Running the app

Any help with this problem will be greatly appreciated! Thanks in advance.

Answer Source

Avoid putting all your subviews directly inside the scrollView. The autolayout will break apart. You need to :

  • Add a UIView inside the UIScrollView, with constraints 0-0-0-0 to leading-top-bottom-trailing to the UIScrollView, and put all your subViews inside that UIView.

After that, you need to set the contentSize of your UIScrollView by code.

Also, you can:

  • add missing constraints to see what is missing.

enter image description here

  • The left panel in UIStoryBoard (Document Outline), you can press the red arrow showing up to see what is missing or conflicting.

enter image description here

Check out Apple documentation for more details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download