Question

I have a Button1 outside ScrollView and another Button2 inside scrollview. I am using storyboard. I have used drag drop segue from both buttons to a different view. Button1 works fine, the problem is with Button2, it doesnt work no matter how many times I click, it only works when I click and drag (strange!!). When I troubleshooted I found that whenever I create an outlet of scrollview it behaves this way, if I remove the connection of scrollview to the outlet it works fine, but I need the outlet for the scrollview to work. Any solution on how I can get this done? This is a sample code of my view did load 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 defined the outlet

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

Note that Button2 Click does work but on click and drag/swipe not on single or multiple clicks.

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

Était-ce utile?

La solution 2

I figured out the problem was because of adding tap gesture, because when your button is inside scrollview the first tap it works for scrollview and not button, so all I had to do is check if touch is for button or not. Here is the code that fixed this issue.

-(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 delegate UIGestureRecognizerDelegate and in viewdidload add this line in the end

singleTap.delegate = self;

Autres conseils

Check out that your button 2 is inside the bounds of scrollview. If button's any part will lie outside the parent container's frame, it will not respond to any events. Test it by changing color of background of the scrollview. Also you can set button2.clipToBounds = YES. If button will be out of bounds, that part will be clipped.

I had similar issue. What I did was remove entire hierarchy involving problematic view (in this case Button 2 and scroll view), then recreate them. Then recreate outlets and IBActions.

This works because storyboard is an xml document and sometimes xcode messes things up in writing it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top