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{
         //....
  }

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.


I resolve it

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

Since the problem is to put a button UIButton


确保你的滚动视图没有吞咽触摸:检查这个其他SO问题

链接地址: http://www.djcxy.com/p/66250.html

上一篇: 如何为android 2.X使用addPreferencesFromResource?

下一篇: UIButton在uiscrollView中不起作用