Why UIButton actions are not called when the UIButton is inside a UIScrollView

I created a panel similar to a panel containing the following buttons:

-(void)configurePanel{

self.criteriaPanel.scrollEnabled=YES;
self.criteriaPanel=[[UIScrollView alloc] initWithFrame:CGRectMake(
                                                                0.0f, 
                                                                0.0f, 
                                                                [UIScreen mainScreen].bounds.size.width,
                                                                (BUTTON_HEIGHT+(3*BUTTON_Y_OFFSET))
                                                                )];

UIView *scrollViewContent=[[UIView alloc]init];
NSArray *criteriaTypeButtonTitles=[NSArray arrayWithObjects:
                                 @"Option1",
                                 @"Option2",
                                 @"Option3",
                                 @"Option4",
                                 @"Option5",
                                 @"Option6",
                                 nil
                                 ];

for (int i=0; i<[criteriaTypeButtonTitles count]; i++) {
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  button.tag=i;
  button.frame = CGRectMake(
                          (((BUTTON_X_OFFSET*i)+(BUTTON_WIDTH*i))+BUTTON_X_OFFSET)
                          , BUTTON_Y_OFFSET
                          , BUTTON_WIDTH
                          , BUTTON_HEIGHT
                          );  
  [button setTitle:[criteriaTypeButtonTitles objectAtIndex:i] forState:UIControlStateNormal];
  [button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
  button.titleLabel.font=[UIFont systemFontOfSize:13.0f];
  [button setBackgroundColor:[UIColor grayColor]];
  [button.layer setCornerRadius:10.0f];
  [button.layer setBorderWidth:1.0f];
  [button addTarget:self action:@selector(sortByButtonTap:) forControlEvents:UIControlEventTouchDown];
  [scrollViewContent addSubview:button];
}

//based upon # of buttons
self.criteriaPanel.contentSize=CGSizeMake(
                                        (([criteriaTypeButtonTitles count]*BUTTON_WIDTH)+([criteriaTypeButtonTitles count]*BUTTON_X_OFFSET)),
                                        (BUTTON_HEIGHT+(2*BUTTON_Y_OFFSET)));
[self.criteriaPanel addSubview:scrollViewContent];
[self.view addSubview:self.criteriaPanel];
}

The panel displays correctly and scrolls, but click events (sortByButtonTap :) for buttons are never called. I suspect this is due to the buttons contained in the view contained in the scrollview. After reading a number of other questions and documents, I still cannot understand what a solution should be.

EDIT: I experimented with adding buttons to the UIScrollView (self.criteriaPanel) directly, and the taps button calls sortByButtonTap: so there’s something to do with the buttons found in scrollViewContent.

+3
source share
5 answers

userInteractionEnabled scrollViewContent :

[scrollViewContent setUserInteractionEnabled:YES];
0

:

[button addTarget:self 
           action:@selector(sortByButtonTap:) 
 forControlEvents:UIControlEventTouchDown];

:

[button addTarget:self 
           action:@selector(sortByButtonTap:) 
 forControlEvents:UIControlEventTouchUpInside];
0

, :

[button setExclusiveTouch:YES];

, , .

0

,

UIButton Scrollview

    scrollview.pagingEnabled = YES;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake();
    [button setTitle:@"X" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnCloseClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(btnCloseDraged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
    [button addTarget:self action:@selector(btnCloseDragedEnd:withEvent:) forControlEvents:UIControlEventTouchDragExit];
    [scrollview addSubview:button];
    scrollview.userInteractionEnabled = YES;

.

, .

0

, , UIControlEvent UIButton. UIControlEventTouchDown UIControlEventTouchUpInside. UIButton Interface Builder, UIControlEvent:

enter image description here

:

Otherwise, yours UIScrollViewmanages the interactions because he needs to know if you want to scroll through it. If you place it UIButtonsinside yours UIScrollView, they will be available, but always with a slight delay. This is why you should change the settings for UIScrollView:

You can disconnect delays content touchesand UIButtonswill be available immediately

enter image description here

0
source

All Articles