UserInteraction enable for subview only

后端 未结 1 1743
旧时难觅i
旧时难觅i 2021-01-05 00:36

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.

相关标签:
1条回答
  • 2021-01-05 01:13

    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
    
    0 讨论(0)
提交回复
热议问题