2
votes

I have a scrollview which has two subviews (iPad). The view hierarchy is as follows:

-- UIScrollView
   -- UIView1
   -- UIView2

The frame of the UIScrollView is the size of the screen, the frame of UIView1 is also the size of the screen, but the frame of UIView2 is (0,0,768,2000).

The scrollview doesn't scroll vertically. According to the apple documentation, the scrollview should automatically set its content size. Can anyone help me out with this issue as why the content size is not being set properly ?

P:S: When I use a single view inside the scrollview and set a proper vertical constraint, it scrolls properly.

6
How do you set the constraints for each of the subviews?Valent Richie
When you embed a view into a UIScrollView in InterfaceBuilder, then there's a constraint automatically set. If your view is "longer" than the screen in portrait, it wont scroll at all. To get UIScrollView going in AutoLayout look into your constraints. Find "Vertical Space - Scroll View - View" and set it from "constant" to "auto"Basant
show your views' constarintsNikita Took

6 Answers

2
votes

Scrollview won't scroll if you enable autolayouts. Technically when you scroll, all the elements in the scrollview change their position. So use auto layout if you are fixing the position of the elements in the scrollview.

Instead use a UIView as a container view inside a scrollview which contains all other objects like button , label, imageview etc. And then you will be able to scroll.

Check the below link for more details: https://developer.apple.com/library/ios/technotes/tn2154/_index.html

In your problem try to put proper values of content size. Also check if the vertical scrolling is enabled or not.

2
votes

The question is old, but the correct answer would be to make sure the autolayout constraints are all set.

Vertically, you should have some constraint pinning your View1 to the Scroll View Top, another one pinning your View2 to the Scroll View Bottom, and one third one setting the vertical space between View1 and View2. Also check that the views themselves (View1 and View2) have constraints for their heights.

Horizontally, pin one of the views to the Scroll View in both leading and trailing space, and the other view left and right to the first one (so they will have equal widths).

Once all of those are set, the content should scroll correctly.

2
votes
  1. Use contentSize property of scroll view in code. The best way is to use 'viewDidLayoutSubviews' method

    • (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; _scrollView.contentSize = CGSizeMake(568, 594); }

2.Always make horizontal space >=0 between lowest of inner views and a scroll view. Automatically, auto layout make suggestions like '-160' and this negative space will not scroll.

0
votes

If the Content size is more than the scroll view frame then only scroll view scrolls otherwise not .

0
votes

Main Point : Mistake is in set 'contentSize' of 'UIScrollView', you need to set manually.

As seem in your Question

scroll view frame = screen frame,
view 1 frame = screen frame,

Its okay but

view 2 frame = (0,0,768,2000), then it overlapping on first view because your view2 x,y position is (0,0), so may be your first view will not display.

If you want to vertically add view 1 after view 2 then your view 2 frame should be

view2.frame = CGRectMake(0,view1.frame.origin.y + view1.frame.size.height , 768,2000);

And after added both of view then you need to manage contentSize of UIScrollView , in your case such like

scrollView.contentSize = CGSizeMake(768, view1.frame.origin.y + view1.frame.size.height + 2000)

I put simple logic, That may be helpful in your case:

0
votes

How to enable scrollview scroll automatically when used with AutoLayout:

Please see the below link which help you to fix it correctly:

https://medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7

Question for above help guide: Which view is parent view? scrollView or parent of scrollView?

Answer: - Parent Of ScrollView (Give equal with and Equal height to view which is parent of scrollview)