[Solved] Can we put a UIButton in a UIScrollView and vice versa in iPhone


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.

1

solved Can we put a UIButton in a UIScrollView and vice versa in iPhone