UIActionSheet Dismiss when click/ tap outside

青春壹個敷衍的年華 提交于 2019-12-03 17:36:20

An even more simpler solution would be to just add a Cancel button. This will automatically enable tap-background-to-dismiss: (Swift)

alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

Try this magic:

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

And this method:

-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.actionSheet];
    if (p.y < 0) {
        [self actionPickerDone:nil];
    }
}

I have an advice that you create a transparent button and make it the same size of your view, and send it back. Create a method to dismiss the action sheet and hook it up with your big button. I never tried this but I think it's worth trying because if that works, you can set it up as a generic method on your view to dismiss the keyboard when user tap outside of the textfield(according to HIG) and also UIActionSheet.

However I don't know whether it is appropriate to dismiss the UIActionSheet the same way as popover. Doesn't action sheet require some input no matter what? Otherwise why apple provide you with a cancel button?

add a toolbar with button to sismiss datePicker and actionSheet

toolbarPicker = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        toolbarPicker.barStyle=UIBarStyleBlackOpaque;
        [toolbarPicker sizeToFit];
        NSMutableArray *itemsBar = [[NSMutableArray alloc] init];
        //calls DoneClicked
        UIBarButtonItem *bbitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DoneClicked)];
        [itemsBar addObject:bbitem];
        [toolbarPicker setItems:itemsBar animated:YES];

        [menu addSubview:toolbarPicker];

and add these actions for done button

-(BOOL)closeDatePicker:(id)sender{
[menu dismissWithClickedButtonIndex:0 animated:YES];
[txtDate resignFirstResponder];

return YES;

}

//action when done button is clicked -(IBAction)DoneClicked{ [self closeDatePicker:self]; menu.frame=CGRectMake(0, 44, 320, 416);

}

as per IOS 6, you can implement it by :

  1. check the buttonIndex ( only run your stuff if buttonIndex>=0). Tapping outside actionsheet will make buttonindex = -1.
  2. implement didDismissWithButtonIndex

Note that this is only works after you implement UIActionSheetDelegate.

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSLog(@"action sheet was dismissed");
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //if user click outside the action sheet, button index will be -1
    if(buttonIndex>0)
    {
        NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!