ios UIActionSheet 取消按钮奇怪的行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1197746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIActionSheet cancel button strange behaviour
提问by nevan king
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.
我有一个 UIBarButtonItem 打开一个操作表,为用户提供关于做什么的选择。除非我尝试单击“取消”按钮,否则一切都按预期工作。按钮的目标似乎已从应有的位置向上移动。我只能通过单击“取消”和“确定”按钮中间的某个位置来激活它。
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
我已经在其他应用程序中尝试过操作表,它们工作得很好,所以这不仅仅是我的大拇指。操作表在 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];
}
回答by nevan king
Instead of passing the current view controller's view to the action sheet, use the showFromTabBar:
method of UIActionSheet
.
不要将当前视图控制器的视图传递给操作表,而是使用 的showFromTabBar:
方法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
如果您使用的是工具栏,请改用该showFromToolbar:
方法。您需要对工具栏的引用,很可能是 ivar
[actionSheet showFromToolbar:self.myToolbar];
My Old AnswerAlso 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.
2008 年 12 月 1 日下午 10:22 Tom Saxton:我又看了一些这个 bug,它似乎是 tabbar 的问题。
如果您从作为 UITabViewController 子级的视图控制器调用 UIActionSheet 的 [sheet showInView:self.view],则取消按钮上的命中测试将在 UIActionSheet 位于选项卡栏视图上方的那部分失败。
如果您改为传入 UITabBarController 的视图,则 UIActionSheet 会按预期运行。
注意:在 iPhone OS 2.1 及更早版本中,当您传递子视图时 UIActionSheet 从标签栏的顶部出现,但在 2.2 中,它从标签栏的底部出现,因此覆盖了标签视图。
http://openradar.appspot.com/6410780
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];
回答by driveguy
回答by Corey Floyd
Instead use:
而是使用:
[sheet showFromTabBar:theTabBar];
回答by Viggnesh
Here is the fix.Try this:
这是修复。试试这个:
[actionsheet showInView:[UIApplication sharedApplication].keyWindow];
回答by RawMean
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.
即,使用 showFromTabBar (这就是它存在的原因),并且您不需要 Nathan 指出的 parentViewController (实际上, self.parentViewController.tabBarController.tabBar 为我返回 nil 。
回答by Mickey
FYI - had the same problem with UIDocumentInteractionController's actionsheet stepping on the tabbar. Used the following to fix.
仅供参考 - UIDocumentInteractionController 的 actionsheet 踩在标签栏上也有同样的问题。使用以下修复。
UIViewController *parentView = [[self parentViewController] parentViewController];
[docController presentOptionsMenuFromRect: rect inView: parentView.view animated:YES];
回答by jayesh kavathiya
write simplite code
写简单的代码
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
this work fine
这工作很好