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.

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];
}
24503 次浏览

不要将当前视图控制器的视图传递给操作表,而是使用 UIActionSheetshowFromTabBar:方法。

正确的方式
这将给出正确的可触摸区域:

[actionSheet showFromTabBar:self.tabBarController.tabBar];

错误的方向
This will put the tappable area in the wrong place (if you're using a tab bar or toolbar):

[actionSheet showInView:self.view];

如果您使用的是工具栏 ,请改用 showFromToolbar:方法。您需要一个对工具栏的引用,很可能是一个 ivar

[actionSheet showFromToolbar:self.myToolbar];

我的老答案同样有效,但是很粗俗:

刚刚找到一个可能的答案:

01-Dec-200810:22 PM Tom Saxton: 我又看了一下这个 bug,它似乎是标签栏的一个问题。

如果您从 UITabViewController 的子视图控制器调用 UIActionsheet 的[ sheet showInView: self. view ] ,那么在位于选项卡视图上方的 UIActionsheet 部分,对取消按钮的点击测试将失败。

如果您转而传入 UITabBarController 的视图,那么 UIActionsheet 将按照预期行事。

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

编辑: 当我将视图更改为选项卡栏的视图时,它正常工作

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

我在 here上找到了一个可行的答案。

使用方法: [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...

取而代之的是:

[sheet showFromTabBar:theTabBar];

仅供参考—— UIDocumentInteractionController 的操作表在选项卡上出现了同样的问题。

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

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.

这是解决办法,试试这个:

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

编写简单的代码

 actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

这个工作很好