2

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

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
  • Why stray from the standard interface paradigm? You will likely cause some confusion to users if you do this without redesigning the presentation format and UI of the UIActionSheet – Chris Wagner Dec 30 '11 at 20:55

2 Answers2

8
// 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.

Community
  • 1
  • 1
dgund
  • 3,459
  • 4
  • 39
  • 64
  • [self.window addGestureRecognizer:tap]; is much more reliable because it means taps on the nav bar or status bar also dismiss. – malhal Feb 02 '13 at 20:52
1
  1. Capture tap button in the parent view (via UITapGestureRecognizer) or on the parent view controller (via overriding touchesBegan: method) depending on your implementation.
  2. If necessary capture location of the tap using [UITouch locationInView:], or [UITapGestureRecognizer locationInView:] method.
  3. (If tapped location is not too far from your action sheet), dismiss your action sheet by calling [UIAction dismissWithClickedButtonIndex:animated:].
barley
  • 4,403
  • 25
  • 28