Orkhan Alizade
Orkhan Alizade

Reputation: 7549

UIScrollView does not Scroll completely

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?

Upvotes: 0

Views: 1351

Answers (4)

GaétanZ
GaétanZ

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 :

  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

Upvotes: 0

Nevs12
Nevs12

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:

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

Upvotes: 1

Yuvrajsinh
Yuvrajsinh

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

Sohel L.
Sohel L.

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

Related Questions