2

When I create a Scrollview on my scene, and then add a button to scene in IB. Then I go into the code, set the content size, enable user interaction and add another button. When I run the program in the simulator the Scrollview does not work, if I remove the button that is in IB on the scene it works just fine. Is it not possible to add items to the scrollview both in IB and programmatically?

EDIT: I thought it may be something in the app I already had. So I decided that I would create anew project and all it has in it is code, and the scene picture below. It is indeed added below the ScrollView.

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setTitle:@"My Button" forState:UIControlStateNormal];
myButton.frame = CGRectMake(100, 100, 150, 50);
[scrollView addSubview:myButton];
scrollView.userInteractionEnabled = YES;
[scrollView setContentSize:CGSizeMake(320, 1000)];

enter image description here

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
ios85
  • 2,104
  • 7
  • 37
  • 55
  • yes u can add both ways.. ur code for adding programmatically is fine.. make sure in xib, button is added inside scrollView not just below the scrollView.. if possible show us the xib.. – vishy Oct 15 '12 at 11:32
  • what is the frame of your scrollview? – Ankit Oct 15 '12 at 11:32

5 Answers5

4

Here your scrollView is not scrolling due to the autoLayout, uncheck the auto Layout if you are not using.

I Just made a similar to your requirement. It is working fine, and after allowing autoLayout it just stopped scrolling.

The auto layout constraints fits to the visible part of the screen, if the objects in scrollView are more then screen size, it will scroll.

So my suggestion if you are not using autoLayout just uncheck it, and works fine.

vishy
  • 3,241
  • 1
  • 20
  • 25
  • Didnt, even think about auto layout. Good catch. Is there any way around this because I am using auto layout in my program. The only thing I can think of is that the whole scrollview will need to be built programmatically. – ios85 Oct 15 '12 at 12:15
  • if ur scrollView content height is fixed for 1000, u can set the constraints values to support that.. – vishy Oct 15 '12 at 12:35
  • I havent been able set any constraint that allows the Scrollview to work with Auto Layout. Were you able to get this to work using constraints? – ios85 Oct 15 '12 at 14:16
  • nope still trying.. if you want just delete constraints within scrollView and try.. for others you can use constraints.. – vishy Oct 15 '12 at 14:22
  • I found this on Apple's site. http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html#//apple_ref/doc/uid/TP40012166-CH1-SW19 It references the UIScrollView Problem but I cant get the solutions to work. – ios85 Oct 15 '12 at 19:15
4

Here is an helpful link: http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html#//apple_ref/doc/uid/TP40012166-CH1-SW19

Basically what this says is that, with auto-layout you don't and shouldn't have to use setContentSize:. Instead your inner view should have it's edges snapped to the edges of the scrollview. Let me demonstrate how I solved it.

scrollBackground : a view that contains every other view that needs to scroll in the scrollview.

[scrollview addSubview:scrollBackground];

[scrollview addConstraints:[NSLayoutConstraint 
       constraintsWithVisualFormat:@"V:|[scrollBackground(1000.0f)]|" 
       options:0 metrics:nil 
       views:NSDictionaryOfVariableBindings(scrollBackground)]];

[scrollview addConstraints:[NSLayoutConstraint 
       constraintsWithVisualFormat:@"H:|[scrollBackground(==scrollview)]|" 
       options:0 metrics:nil 
       views:NSDictionaryOfVariableBindings(scrollBackground)]];

The key here being those | at beginning and end of the VisualFormat String. These will tell the scrollview what is the contentSize. Of course if you have a contentSize smaller than the frame size of the scrollview it won't scroll in that direction. In my example this is true for width. It only scrolls up and down.

boulette
  • 113
  • 1
  • 5
1

Make sure you set your content size in viewDidLoad or at some point after the view has already been loaded from the nib file.

As vishy Pointed out, your Button should be part of the ScrollView's Hierarchy, else you'll just be scrolling an empty view.

ekinsol
  • 501
  • 1
  • 3
  • 9
1

In the case you posted, the scrollview will not scroll because all of the content is visible. Try changing that last line to:

[scrollView setContentSize:CGSizeMake(50, 50)];

and it will start scrolling, because the content size is smaller than all of the content in the view.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

check d2burke answer in this question you should place your code in viewWillLayoutSubviews instead of viewDidAppear

Community
  • 1
  • 1
Mouhamad Lamaa
  • 884
  • 2
  • 12
  • 26