Search code examples
iphoneiosxcodeuiactionsheet

Hide UIActionSheet when user taps on above view in iPhone


I want to set action on action sheet such that when user taps on above of action sheet it get dismissed.
I want to this because I don't want to add cancel button in actionsheet. Instead of cancel button user can cancel action sheet by tapping on rest area view.
Any suggestion how to do this in iPhone.
Thanks


Solution

  • // For detecting taps outside of the alert view
    -(void)tapOut:(UIGestureRecognizer *)gestureRecognizer
    {
        CGPoint p = [gestureRecognizer locationInView:self];
        if (p.y < 0)
        {   // They tapped outside
            [self dismissWithClickedButtonIndex:0 animated:YES];
        }
    }
    
    -(void) showFromTabBar:(UITabBar *)view
    {
        [super showFromTabBar:view];
    
        // Capture taps outside the bounds of this alert view
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
        tap.cancelsTouchesInView = NO; // So that legit taps on the table bubble up to the tableview
        [self.superview addGestureRecognizer:tap];
        [tap release];
    }
    

    See here.

    Note, this already occurs on the iPad.