Search code examples
uiviewuibuttonuikituicontrolcliptobounds

UIButton is placed out of bounds - should it work?


I have a UIView, and from inside the view I've attached a UIButton to be aligned to the bottom of it. Naturally I've set clipToBounds to NO, so that the button is visible.

The button is aligned beautifully (with constraints) but alas, it will not respond to taps (not even highlighted).

Is that even possible to do? or must it reside in the parent view in order to respond?

Thanks.


Solution

  • Sure it's possible.

    Implement this in the view that contains the button:

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        BOOL pointInside = CGRectContainsPoint(self.button.frame, point);
        return pointInside;
    }
    

    If pointInside is YES, the view will respond to the tap even though it's outside its' bounds.