David Nix
David Nix

Reputation: 3334

Pass touch from UIScrollView to superview (UIButton)

Possibly a duplicate, but I couldn't find an exact answer to my problem searching SO tonight...

I have a UIButton which contains a UIScrollView, so the button is the superview.

All I want to do is pass a single-tap event from the UIScrollView subview to the UIButton superview, and have the UIButton handle it as it would if the user tapped the button directly.

Setting the scroll view's userInteractionEnabled property to NO won't work for me because there is content the user can scroll. Doing so would defeat the purpose of the scroll view.

Is this possible? Thanks!

Upvotes: 2

Views: 5014

Answers (3)

Vignesh
Vignesh

Reputation: 10251

Create a subclass of scrollview and override

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
return YES;
}

Also subclass the uibutton(Your superview) and handle touches in touches began.

Upvotes: 1

Kevin Low
Kevin Low

Reputation: 2682

Like huoxinbird said, it's definitely not common to lay your views like that.

In the case where you are solidly adamant about this, you could use UITapGestureRecognizer and have it call the button's target and selector.

If you wanted to include the button highlight and such, then you may have to subclass UITapGestureRecognizer and forward the touchesEnded to the button.

Upvotes: 2

huoxinbird
huoxinbird

Reputation: 530

Usually you don't put scrollview within a button. Instead consider using a UIView as the superview of scrollview. Or tell us more about your use case, so I can understand why you have to design in this way.

Upvotes: 0

Related Questions