Monday, June 4, 2012

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")
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
destructiveButtonTitle:NSLocalizedString(@"Open Link", @"Open Link")
otherButtonTitles:nil];

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


Source: Tips4all

6 comments:

  1. 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.


    http://openradar.appspot.com/6410780

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

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

    ReplyDelete
  2. I found an answer over here that works:

    http://stackoverflow.com/questions/1174734/how-do-you-control-action-sheet-animation-direction

    using: [filterActionSheet showInView:[self.view window]];

    i tried a few ways to get to my tab bar and they way this app is set up it seem convoluted...

    ReplyDelete
  3. Instead use:

    [sheet showFromTabBar:theTabBar];

    ReplyDelete
  4. Here is the fix.Try this:

    [actionsheet showInView:[UIApplication sharedApplication].keyWindow];

    ReplyDelete
  5. I think a combination of three of the answers is the right way of handling this:

    [actionSheet showFromTabBar:self.tabBarController.tabBar];


    i.e., use showFromTabBar (that's why it exists) and you don't need the parentViewController as Nathan pointed out (in fact, self.parentViewController.tabBarController.tabBar returns nil for me.

    ReplyDelete
  6. FYI - had the same problem with UIDocumentInteractionController's actionsheet stepping on the tabbar. Used the following to fix.

    UIViewController *parentView = [[self parentViewController] parentViewController];
    [docController presentOptionsMenuFromRect: rect inView: parentView.view animated:YES];

    ReplyDelete