Reputation: 7549
I have a ViewController. In it I put ScrollView with the View(contentView). Later I drag from contentView to View and set Equal Height. Now it scrolls, but not fully.
As you see there are it has continue below the textView, but it doesn't scrolls. How can I fix it?
Upvotes: 0
Views: 1351
Reputation: 4930
According to this link (thanks to this Matt's answer first), UIScrollView acts differently with AutoLayout than the other views.
Subviews of a scrollView set their constraints from the contentView of the scrollView and not the scrollview directly. This allows the content to scroll.
So :
It should look like this :
Hope this helps
Read this (from Matt once again) for further informations
Upvotes: 0
Reputation: 489
UIScrollView is able to automatically calculate it's content height and width, but you need to help it with this. To do so you need to:
contentView
(in your case) to all sides of superview (which is Scroll View
).contentView
to calculate it's sizes. Here is a small mistake in your approach. You've set height of the contentView
equal to View
's height. So basically Scroll View
's contentSize.height is the same as View
's height. Which is not really what you want with dynamic content.Usually you want to set width of the contentView
equal to View
's width and do not set contentView
's height. Instead you want to bind subviews of contentView
to their superview in such a way that their superview (contentView
) will calculate it's height automatically.
In your case I would bind:
pizza.jpg
to left-top-right of superview (height of pizza.jpg
will be set from intrinsic image size);SAMPLE TITLE
label - left-right to superview; top to pizza.jpg
image;Text View
- left-bottom-right to superview; top to SAMPLE TITLE
label; set a fixed height.In this case contentView
will define needed height by itself. Scroll View
will set it's contentSize accordingly.
And your screen will be able to scroll vertically (it should be) ;)
Upvotes: 1
Reputation: 4584
To use Autolayout
with UIScrollView
is tricky.
In your code you have to update height constraint for your contentView by calculating height of subviews
of contentView
and that will automatically update the contentSize
for your ScrollView
and you can scroll through all subviews.
For more info to use Autolayout+UIScrollView your can read this.
Upvotes: 0
Reputation: 9540
You need to set the contentsize of the scrollview. Use the below code to do that:
func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.contentView.frame.size.height);
}
Upvotes: 0