nevan king nevan king - 1 year ago 45
Objective-C Question

UIActionSheet cancel button strange behaviour

I have a UIBarButtonItem opening an action sheet to offer users choices about what to do. Everything works as expected unless I try to click on the "Cancel" button. The target of the button appears to have moved up from where it should be. I can only activate it by clicking somewhere in the middle of the "Cancel" and "Ok" buttons.

alt text

I've tried at action sheets in other applications and they work fine, so it's not just my big thumb. The action sheet is opening in a UIViewController

- (void)showOpenOptions
UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:NSLocalizedString(@"Open link in external application?", @"Open in external application")
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
destructiveButtonTitle:NSLocalizedString(@"Open Link", @"Open Link")

[sheet showInView:self.view];
[sheet release];

Answer Source

Instead of passing the current view controller's view to the action sheet, use the showFromTabBar: method of UIActionSheet.

The Right Way
This will give the correct tappable area:

[actionSheet showFromTabBar:self.tabBarController.tabBar];

The Wrong Way
This will put the tappable area in the wrong place (if you're using a tab bar or toolbar):

[actionSheet showInView:self.view];

If you're using a toolbar, use the showFromToolbar: method instead. You'll need a reference to the toolbar, most likely an ivar

[actionSheet showFromToolbar:self.myToolbar];

My Old Answer Also works, but is hacky:

Just found a possible answer:

01-Dec-2008 10:22 PM Tom Saxton: I looked at this bug some more, and it seems to be an issue with the tabbar.

If you call UIActionSheet's [sheet showInView:self.view] from a view controller that is a child of a UITabViewController, then the hit testing on the cancel button fails in that portion of the UIActionSheet that lies above the tabbar's view.

If you instead pass in the UITabBarController's view, then the UIActionSheet acts as expected.

NOTE: in iPhone OS 2.1 and earlier, the UIActionSheet came up from the top of the tab bar when you pass the child view, but in 2.2, it comes up from the bottom of the tab bar, and thus covers the tab view.

Edit: It works correctly when I change the view to be the tab bar's view

[sheet showInView:self.parentViewController.tabBarController.view];