-4

Can someone provide a code example for the given scenario?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124

2 Answers2

1
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.

texmex5
  • 4,354
  • 1
  • 26
  • 28
0

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.

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Arun
  • 483
  • 6
  • 20