Sergiy Lyahovchuk
Sergiy Lyahovchuk

Reputation: 1

UIButton above two UIView is not pressed

I have two UIViews. On second "UIView" I have one UIButton, but it must be over the both views. When I click lower middle UIButton - event is fire, but when I click above the middle - nothing works.

How to fix it?

example image

Upvotes: 0

Views: 48

Answers (2)

tuledev
tuledev

Reputation: 10327

You should create a subclass of UIView for UIView2 and override hitTest like this:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{   
    if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
        for (UIView *subview in self.subviews.reverseObjectEnumerator) {
            CGPoint subPoint = [subview convertPoint:point fromView:self];
            UIView *result = [subview hitTest:subPoint withEvent:event];
            if (result != nil) {
                return result;
            }
        }
    }

    return nil;
}

Upvotes: 0

haluzak
haluzak

Reputation: 1143

move the button outside of both views and center it vertically

Upvotes: 1

Related Questions