Can we put a UIButton in a UIScrollView and vice versa in iPhone
Can someone provide a code example for the given scenario开发者_JAVA百科?
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.
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.
精彩评论