我有一个UIToolbar添加到视图中,工具栏有几个UIBarButtonItem。其中一个按钮将调出UIDocumentInteractionController Open In菜单。在iPhone中,"打开方式"菜单的工作方式类似于UIActionSheet,它将从屏幕底部显示并禁用除自身以外的所有其他按钮(屏幕的其他部分将被浅灰色阴影掩盖,你知道我的意思)。
我的问题是,当"打开方式"菜单出现时,UIToolBar 中的所有按钮都会消失,有点像 iOS 的"自动隐藏"。关闭"打开方式"菜单后,按钮将返回。我想要的只是在打开菜单时保持按钮可见。
此问题仅出现在iOS6(当然还有iPhone)中,因为在iOS5中,"打开方式"菜单具有不同的样式。我不太确定是否可以更改此行为。但是,Adobe PDF阅读器应用程序似乎正在做与我相同的事情,但工具栏项目永远不会消失。
我用来调出"打开方式"菜单的代码是这样的:
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.topBar animated:YES]
self.topBar是我之前提到的工具栏,我也尝试了self.view,self.view.window等,但没有一个有效。
我错过了吗?还是有一些解决方法?
根据需要添加更多代码:
UIToolbar 被添加到 XIB 文件中,我用 UIButton 自定义一个 UIBarbuttonItem,如下所示:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[button addTarget:self action:@selector(doSth) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *tItem = [[UIBarButtonItem alloc] initWithCustomView:button];
然后我将 tItem 添加到 UIToolbar 的 items 数组中:
NSArray *items = [NSArray arrayWithObject:tItem];
topBar.items = items;
"doSth"方法只做一件事,只需初始化一个UIDocumentInteractionController并显示"打开方式"菜单:
UIDocumentInteractionController documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
[documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.topBar animated:YES];
当然,工具栏中还有更多栏按钮项。因此,当显示"打开方式"菜单时,所有按钮都"隐藏"。我认为苹果可能会通过设计来做到这一点,但我想知道是否有一些解决方法来改变这种行为。
这里的秘诀是使用 UINavigationBar 而不是 UIToolbar。正式地,UIToolbars应该在屏幕底部使用,UINavigationBars在顶部。(但是,这在以前可能是不可能的,因为直到iOS5,UINavigationBar的每一端才可以有多个项目。
这似乎是苹果"按照设计"的。有关此功能可能有用的示例,请在 Dropbox 应用中查看文件时查看底部工具栏。 (如果他们没有隐藏项目,它们将直接落在"取消"按钮的接缝处。