-1

In ios 7.0 I just want to close that action sheet whenever the user presses outside of that popover.I am showing that action sheet on button click and using this code trying to get some tap gesture on actionsheet.window

-(IBAction)buttonClicked:(id)sender{
    NSLog(@"button clicked");
    NSLog(@"action sheet ");
    actionSheet = [[UIActionSheet alloc] initWithTitle:nil                                                         delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Rename",@"Delete",@"Cancel" ,nil];


    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        }
    }
    //  [actionSheet setBackgroundColor:[UIColor whiteColor]];
    [actionSheet showInView:self.view];


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
    tap.cancelsTouchesInView = NO;
    [actionSheet.window addGestureRecognizer:tap];

    [actionSheet.window addGestureRecognizer:tap];

}

-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"in tap out");
    CGPoint p = [gestureRecognizer locationInView:actionSheet];
    if (p.y < 0) {
        NSLog(@" p.y < 0 ");
    }
}

But am not getting this tap gesture.Please help me in solving this.

Shamas S
  • 7,507
  • 10
  • 46
  • 58
Snigdha
  • 29
  • 1
  • 5

4 Answers4

1

Hi this is my solution to dismiss the actionsheet when you tap outside

First create one view to handle the tap gesture and the action sheet

@implementation MyClass
{
    UIActionSheet *_myActionSheet;
    UIView *_myTapView;
}

on the actionsheet delegate method

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
    [_myTapView removeFromSuperview];
    _myTapView = [[UIView alloc] initWithFrame:actionSheet.superview.bounds];
    _myTapView.backgroundColor = [UIColor clearColor];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
    tap.cancelsTouchesInView = NO;
    [_myTapView addGestureRecognizer:tap];

    [actionSheet.superview insertSubview:_myTapView belowSubview:actionSheet];
}

and handle the tap gesture

- (void)tapOut:(UIGestureRecognizer *)gestureRecognizer
{
//dismiss the action sheet here
    [_myActionSheet dismissWithClickedButtonIndex:<index> animated:YES];
}
0

You may try to add a tapgesture on different then actionSheet.window.

Please try to add tapgesture on self.superView

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
tap.cancelsTouchesInView = NO; 
[self.superview addGestureRecognizer:tap];
//OR
[actionSheet.superview addGestureRecognizer:tap];

Like this, you can try different thing based on view hierarch.

Update 1

Try to subclass the Actionsheet and define tapgesture in class it self like.

-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self];
    if (p.y < 0) { // They tapped outside
        [self dismissWithClickedButtonIndex:0 animated:YES];
    }
}

-(void) showInView:(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];
}

Enjoy Coding!!

Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
  • Yes I tried even that.. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)]; tap.cancelsTouchesInView = NO; tap.numberOfTapsRequired = 1; actionSheet.superview.userInteractionEnabled = YES; [actionSheet.superview addGestureRecognizer:tap]; but not getting into that method – Snigdha Apr 14 '15 at 10:45
0

The following applies to a subclass of a UIActionSheet

 actionSheet = [[UIActionSheet alloc] initWithTitle:nil                                                         delegate:self
                                 cancelButtonTitle:@"Cancel"
                            destructiveButtonTitle:nil
                                 otherButtonTitles:@"Rename",@"Delete" ,nil];


for (UIView *subview in actionSheet.subviews) {
    if ([subview isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)subview;
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    }
}
//  [actionSheet setBackgroundColor:[UIColor whiteColor]];
[actionSheet showInView:self.view];
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • But am not showing actionsheet from tab bar am using showInView I tried adding [actionSheet.superView addGestureRecognizer:tap] – Snigdha Apr 14 '15 at 11:03
  • i think problem is that the action sheet overlap your whole view with transparent view that why you not get the tap gesture. – Chirag Shah Apr 14 '15 at 11:08
  • Yes then how can we close that when user taps outside of the actionsheet – Snigdha Apr 14 '15 at 11:18
  • Could you help me in solving this? – Snigdha Apr 14 '15 at 14:57
  • i try but didnot find any solution for your issue – Chirag Shah Apr 14 '15 at 14:58
  • I tried adding a subview to action sheet and adding tap gesture for that view but in vian..that subview is not getting added to actionsheet UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; background.backgroundColor = [UIColor redColor]; [actionSheet addSubview:background]; – Snigdha Apr 14 '15 at 15:02
  • hi @Snigdha comment your actionsheet code and put my edited code might it help you. – Chirag Shah Apr 14 '15 at 15:06
  • Hi Thanks a lot by using cancelButtonTitle we can do that. It's amazing.Thankyou so much – Snigdha Apr 14 '15 at 15:30
-1

This code helps you to dismiss the action sheet

[self dismissWithClickedButtonIndex:0 animated:YES];

Button Index '0' is cancel button's button index.

Rajjjjjj
  • 424
  • 3
  • 16