Can someone provide a code example for the given scenario?
-
4You can put any uiview subclass instance into another, but why do you want to put a scrollview inside a button? – Vladimir May 14 '10 at 10:46
-
I like to to do it for the sake of doing it, just to find out how can i do it in iPhone? – Muhammad Maqsoodur Rehman May 14 '10 at 10:53
-
2If you want to find out how to do it, then asking the question defeats your object. – Paul Lynch May 14 '10 at 11:07
-
1question shows no effort – Max MacLeod Oct 22 '12 at 14:44
2 Answers
UIScrollView *scrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0F, 0.0F, 320.0F, 480.0F)] autorelease];
[self.view addSubview:scrollView];
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setTitle:@"Title" forState:UIControlStateNormal];
[button setFrame:CGrectMake(0.0F, 0.0F, 50.0F, 50.0F)];
[scrollView addSubview:button];
If you have to add a subview to a UIButton then you would just to it in the opposite order:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setTitle:@"Title" forState:UIControlStateNormal];
[button setFrame:CGrectMake(0.0F, 0.0F, 50.0F, 50.0F)];
[[self.view addSubview:button];
UIScrollView *scrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0F, 0.0F, 320.0F, 480.0F)] autorelease];
[button addSubview:scrollView];
But the scrollview
will block the touches from the button unless you set userInteractionEnabled
and exclusiveTouch
properties to NO
on the scrollview. But that would defeat the purpose of having a scrollview inside a button I think.

- 4,354
- 1
- 26
- 28
-
Fine!This is the code to set UIButton in UIScrollView. How to do it vice versa?What's the code? – Muhammad Maqsoodur Rehman May 14 '10 at 11:04
And if your button gets unclickable,then just check the content size of your view(self.view in case of IB). It should be greater than or equal to the size of the scrollView. In my case i was setting content size of scrollView as-
self.scrollView.contentSize=CGSizeMake(320,580);
and adding view as subview to the scrollView
[self.scrollView addSubview:self.view];
and didn't set the size of view. So that was my mistake. As default height of view is 480 in case of 3.5" retina display and 568 in case of 4" retina.
So i resolved this by setting content size of my view as-
self.view.frame=CGRectMake(0, 0, 320, 700);
and adding this view as subview of scrollview.

- 12,655
- 23
- 43
- 64

- 483
- 6
- 20