Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton does not work in uiscrollView

I have a scrollView that has a UIView as subview. This has as UIView subview a UIButton. Only the scrollView is connected to the outlet, the rest is all in code. The button does not respond to touches, not turns blue when touched. What can I do to make it work?

This is the code:

   - (void)viewDidLoad
 {
       [super viewDidLoad];

      //......

      self.buttonHome = [UIButton buttonWithType:UIButtonTypeCustom];
      [self.buttonHome addTarget:self action:@selector(pressedHome:)
                                  forControlEvents:UIControlEventTouchUpInside];
      //....
      self.containerView =[[UIView alloc]initWithFrame:self.scrollView.frame];
      self.containerView.userInteractionEnabled=YES;

      [self.scrollView addSubview:self.containerView];
      [self.containerView addSubview:self.buttonHome];
  }

  -(void) pressedHome:(id)sender{
         //....
  }
like image 987
Adriana Carelli Avatar asked Nov 20 '12 14:11

Adriana Carelli


2 Answers

You must set content size of your view. That must be greater than or equal to the content size of scrollView.

Because default size of your view is 320*480 (3.5" retina) and 320*568 (4" retina). So increasing height of view as-

self.view.frame=CGRectMake(0, 0, 320, 700);

Then adding this as the subview of your scrollView.

will get you to the solution.

like image 140
Arun Avatar answered Nov 14 '22 21:11

Arun


I resolve it

  - (BOOL)touchesShouldCancelInContentView:(UIView *)view
  {
       return ![view isKindOfClass:[UIButton class]];
  }

Since the problem is to put a button UIButton

like image 24
Adriana Carelli Avatar answered Nov 14 '22 21:11

Adriana Carelli