0

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.

enter image description here

enter image description here

As you see there are it has continue below the textView, but it doesn't scrolls. How can I fix it?

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79

4 Answers4

1

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:

  1. Bound contentView (in your case) to all sides of superview (which is Scroll View).
  2. Let 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) ;)

Nevs12
  • 599
  • 6
  • 13
  • The correct answer after researching for hours. You saved my day. The key point is, setting the bottom constraint of the most bottom to the bottom of the content view. – Atreidex Jul 23 '20 at 09:01
0

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);

}
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

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.

Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
0

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 :

  1. Add a UIView to your scrollView, this will represent the contentView of your scrollView. Add constraints to top, bottom, trailing, leading from the view to its superView
  2. Interface Builder complains. Here you see the different between a basic view and a scrollView. The reason is a contentView has to be fill to know its size. So add a equal width from the contentView to the scrollView
  3. The contentView knows now its width but not its height. So add your labels and your UIImage as subviews of the contentView. Add constraints from bottom to the top. Don't miss to add a height constraint to the UIImageView.

It should look like this :

enter image description here

Hope this helps

Read this (from Matt once again) for further informations

Community
  • 1
  • 1
GaétanZ
  • 4,870
  • 1
  • 23
  • 30