问题
I'm novice in iPhone development. Can anyone tell me how to dismiss UIActionSheet 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
回答1:
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
回答2:
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))
回答3:
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];
}
}
回答4:
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?
回答5:
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);
}
回答6:
as per IOS 6, you can implement it by :
- check the buttonIndex ( only run your stuff if buttonIndex>=0). Tapping outside actionsheet will make buttonindex = -1.
- 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];
}
}
来源:https://stackoverflow.com/questions/7402548/uiactionsheet-dismiss-when-click-tap-outside