Click button not responding in ScrollView when I create ScrollView output in iOS

I have Button1 outside ScrollView and another Button2 inside scrollview. I use a storyboard. I used drag and drop from both buttons to another view. Button1 works fine, the problem is with Button2, it doesn't work no matter how many times I click, it only works when I click and drag (weird !!). When I was troubleshooting, I found that whenever I create an output from scrollview, it behaves this way, if I remove the scroll connection from the outlet, it works fine, but I need the outlet for scrollview to work. Any decision on how I can do this? This is sample code of my view, booted if it helps

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]; self.scrollView.contentSize =CGSizeMake(320, 500); UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignKeyboard)]; [self.scrollView addGestureRecognizer:singleTap]; singleTap.numberOfTapsRequired = 1; } -(void) resignKeyboard { [self.view endEditing:YES]; } 

This is how I determined the way out

 @property (strong, nonatomic) IBOutlet UIScrollView *scrollView; 

Note that Button2 Click works, but it doesn’t click and drag / project on one or more clicks.

EDIT: I checked and found that it works fine for 6.1 simulator, but not 5.0

+4
source share
3 answers

I realized that the problem is with adding click gestures, because when your button is inside scrollview, the first click on it works for scrollview and not for the button, so all I had to do was check if the button was pressed or not. Here is the code that fixed this problem.

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if (self.scrollView.superview != nil) { if ([touch.view isKindOfClass:[UIButton class]]) { return NO; // ignore the touch } } return YES; // handle the touch } 

Add UIGestureRecognizerDelegate delegate and add this line to the end in viewdidload

 singleTap.delegate = self; 
+1
source

Verify that your button 2 is within scrollview. If any part lies outside the frame of the parent container, it will not respond to any events. Check it out by changing the background color of the scroll. You can also set button2.clipToBounds = YES . If the button is outside, this part will be cropped.

+2
source

I had a similar problem. What I did was remove the whole hierarchy with the problematic presentation (in this case, Button 2 and scroll view), and then recreate them. Then recreate exits and IBActions.

This works because a storyboard is an xml document, and sometimes xcode puts things in writing.

0
source

Source: https://habr.com/ru/post/1499794/


All Articles