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:
And even when I preview the file Main.storyboard before running it looks like this:
But when I actually run the app, the screen is missing the UITextView, even when I alter the constraints in a number of ways:
Any help with this problem will be greatly appreciated! Thanks in advance.
If you want your
scrollView
only scrolls vertically you shouldn't set its height equal to itssuperView
so remove it and just set the width to itssuperView
and then it should calculate the height based on thesubViews
inside it I offer you to drag aUIView
in yourscrollView
and set the constraints to its four sides, and name it containerView , then set its width equal to background view and start laying out your views inside it not inside the scrollview :) If Height of all views in the scrollView is clear, it can infer the scrollView's height in this case you can set a fixed height to yourcontainerView
like800
to get rid of the red lines and check how it works :)Avoid putting all your subviews directly inside the
scrollView
. The autolayout will break apart. You need to :UIView
inside theUIScrollView
, with constraints0
-0
-0
-0
toleading
-top
-bottom
-trailing
to theUIScrollView
, and put all your subViews inside thatUIView
.After that, you need to set the
contentSize
of yourUIScrollView
by code.Also, you can:
add missing constraints
to see what is missing.Check out Apple documentation for more details.
I have noticed some clues you might have to solve which could probably solve the issue. Since the scrollView is extend by its content's elements, you have to explicitly deal with each element in the scrollView:
For imageView on the top is not well constrained, you need to give it a width or aspect ratio. Fix the imageView issue might solve textview height problem, even it's not still is a good start.
TextView bottom anchor is equal to scrollView bottom anchor, but you have to know textView is also kind of scrollView. So it's not reasonable to constraint each other at same time. Because both of them don't have explicitly height. You can try to type some words in the textView which will at least give it height by its content, then the scrollView can detect the textView bottomAnchor. You might see something then.
Your scrollView's height is equal to view's height is also weird, scrollView shouldn't constraint its height at first. Because it can't be "Scroll"View anymore because it's height is constant. you should let its width equal to view, and let the height be decided by its element's height. Then it will be literally a scrollview.
Hope it helps