0

This seems to be a pretty common question but no matter which piece of code or change I make I cannot get it to work. I have a very simple example that I'm working on.

Storyboard is set to 4" portrait. UIScrollView 320px wide x 440px high. It contains a UIView which is 320px wide by 900px high. Application is locked to portrait only but needs to work on both 3.5" and 4" screens.

What do I need to do to get the UIView to scroll vertically within the UIScrollView? Do I need to set a contraint? What should Clip Subviews and Autoresize Subviews be set to and does it make a difference? What should View Mode be set to and does it make a difference? Do I need to programmatically reset the ContentSize of the UIScrollView? If so is this done on the ViewDidLoad event?

I've tried adding vertical contraints to the UIView, the bottom element on the UIView and the UIScrollView itself. I've programmatically set the content size to match the UIView.

I've built UI's in all sorts of other tools and this is just driving me insane for something so simple. If I turn off autolayout it works fine but that's not the solution I'm looking for.

Moth
  • 101
  • 1
  • 10

2 Answers2

4

Ended up getting this answer to work. https://stackoverflow.com/a/16029013/1342779

Specifically my code in the viewDidLoad method:

[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_innerView
                                                           attribute:NSLayoutAttributeBottom
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:_scrollView
                                                           attribute:NSLayoutAttributeBottom
                                                          multiplier:1.0
                                                            constant:0]];
Community
  • 1
  • 1
Moth
  • 101
  • 1
  • 10
0

Take a look at this video. Also, you can try setting the content size of the scrollview problematically as well.

// For verical scrolling
scrollView.contentSize = CGSizeMake(viewToScroll.width, viewToScroll.height * numberOfPages);

// For horizontal scrolling
scrollView.contentSize = CGSizeMake(viewToScroll.width * numberOfPages, viewToScroll.height);
Abhinav
  • 37,684
  • 43
  • 191
  • 309