2

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Basant
  • 51
  • 1
  • 4
  • How do you set the constraints for each of the subviews? – Valent Richie Oct 08 '13 at 14:00
  • 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 Oct 09 '13 at 11:24
  • show your views' constarints – Nikita Took Sep 10 '14 at 04:56

6 Answers6

2

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.

Geekoder
  • 1,531
  • 10
  • 21
  • Putting a content view inside the scrollview is the solution but, what is there are multiple views, like there is a scrollView and lets say two labels which i place directly on the scrollView ? I might be completely wrong here, but there has to be a solution better than this. – Basant Oct 08 '13 at 11:13
  • 2
    Possible solutions to this is UIScrollView with UIView as containerSubView. Than you can add 2 more UIViews or whatever object you need in that containerSubView. This will definitely work. You should be having only one immediate subView to the scrollview that is containerSubView and DONE :) – Geekoder Oct 08 '13 at 11:47
  • Chinab, the solution which you suggested is the one which am using right now. And it works, but am wary about the use of additional views here. And its a very common scenario, we just add some stuff on the scrollview, but we don't add a contentView always. – Basant Oct 08 '13 at 12:10
  • I have tried what you want to achieve. I have added a containerView as subview to the scrollView. Then in the containerView I have added 2 more UIView as subview. Now the size of the containerView should be equal to the content size of scrollView. With auto layouts there should be only one immediate subView to the scrollView to make things easier. – Geekoder Oct 08 '13 at 12:16
  • I saw something similar to what i wanted to do here : [link](http://stackoverflow.com/questions/16825189/auto-layout-uiscrollview-with-subviews-with-dynamic-heights?rq=1) – Basant Oct 08 '13 at 13:50
2

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.

Uzaak
  • 578
  • 3
  • 13
2
  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.

Oleg Tretiakov
  • 159
  • 1
  • 12
0

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:

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • YEs the view's overlap, i have a functionality where i have to keep that view hidden and just fill some data and make a pdf out of it. So thats not an issue but a requirement. Further, i tried setting the contentSize in viewDidload, which i know is not the right approach didnt work. On setting the contentSize in viewDidAppear, again not the right approach, worked on iOS 6 but didn't work on iOS 7. Am sure there is a way, to make autolayout set the contentSize according to the topmost view on the scrollView. – Basant Oct 08 '13 at 11:08
0

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

Tendulkar
  • 5,550
  • 2
  • 27
  • 53
  • You dont need to set the content size, when you are using autolayout, it automatically sets the content size according to the view in the scrollView. – Basant Oct 08 '13 at 11:18
0

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)

Sandip Patel - SM
  • 3,346
  • 29
  • 27