开发者

UIActionSheet Dismiss when click/ tap outside

I'm novice in iPhone development. Can anyone tell me how to dismiss UIA开发者_如何学GoctionSheet control when i tapped outside of it?

In my actionSheet i have only datePicker control and it pops up over tab bar control now what i want whenever user click outside of it, it should dismiss instead of using actionSheet's cancel button. Regards


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];
    }
}


Hi refer below link: How to make a uiactionsheet dismiss when you tap outside eg above it?

http://splinter.com.au/how-to-allow-closing-a-uiactionsheet-by-tappi


In SwiftUI, when using ActionSheet adding an Alert.Button.Cancel achieves the desired result:

@State var isShowingActionSheet: Bool = false
@State var isShowingCamera: Bool = false

.actionSheet(isPresented: $isShowingActionSheet) {
    ActionSheet(
        title: Text("Send an image"),
        buttons: [
            .default(Text("Camera")) {
                isShowingCamera = true
            },
            // This enables the "tap anywhere outside of the action sheet to dismiss it" behavior 
            .cancel()
        ]
    )
}


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);

}


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?


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];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜