Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserInteraction enable for subview only

I have a view and view.UserInteractionenabled = no and a button is added to the view. i need to click the button only . is it possible to enable interaction for button only.

like image 415
Vishnu Avatar asked Nov 16 '12 09:11

Vishnu


1 Answers

A view cannot receive touches unless userInteractionEnabled is YES for the view and all of its superviews up to the UIWindow object.

You can make a subclass of UIView to contain the button, and make it ignore touches outside the button by overriding hitTest:withEvent:. Example:

@interface MyView : UIView

@property (nonatomic, strong) IBOutlet UIButton *button;

@end


@implementation MyView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *subview = [super hitTest:point withEvent:event];
    return subview == self.button ? subview : nil;
}

@end
like image 94
rob mayoff Avatar answered Sep 30 '22 07:09

rob mayoff