QLPreviewController删除或添加UIBarButtonItems [英] QLPreviewController remove or add UIBarButtonItems

查看:377
本文介绍了QLPreviewController删除或添加UIBarButtonItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上看到过很多这样的问题,但似乎没有人真正知道答案?

I have seen this kind of question a lot on the internet but it seems no one really knows the answer?

我正在使用QLPreviewController来显示PDF文档。我首先使用了一个UIWebView,但我建议使用QLPreviewController来代替性能更高的文档。

I am using QLPreviewController for displaying PDF documents. I first used a UIWebView but I was recommended to use QLPreviewController instead for performance reasons with bigger documents.

我想要的是右上角的4个自定义UIBarButtonItem(所以打印按钮是)。

what I want is 4 custom UIBarButtonItem's in the top right (so where the print button is).

我设法在底部设置了一个自定义工具栏,但这不是我想要的。

I managed to get a custom toolbar at the bottom, but that's not really what I want.

考虑到无法在打印按钮的位置添加自定义按钮,我仍然想要删除打印按钮并使用自定义工具栏。

Considering that it is not possible to add custom button at the place of the print button, I still want to remove the printbutton and use the custom toolbar instead.

编辑(解决方案):
我刚刚找到解决方案,但没有更新此帖子,所以这就是我如何解决问题:

我手动添加按钮:

// Create a toolbar to have the buttons at the right side of the navigationBar
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 180, 44.01)];
[toolbar setTranslucent:YES];

// Create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];


// Create button 1
button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(button1Pressed)];
[buttons addObject:button1];

// Create button 2
button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(button2Pressed)];
[buttons addObject:button2];

// Create button 3
button3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(button3Pressed)];
[buttons addObject:button3];

// Create a action button
openButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(openWith)];
[buttons addObject:openButton];

// insert the buttons in the toolbar
[toolbar setItems:buttons animated:NO];

// and put the toolbar in the navigation bar
[[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:toolbar]];


推荐答案

更新:

这在iOS 6中不再有效。快速查看在使用XPC的另一个进程中运行。有关详细信息,请参阅此处。我没有预见到任何自定义QLPreviewController的方法。以下答案仍适用于对iOS 6之前感兴趣的人。

This no longer works in iOS 6. Quick Look runs in another process using XPC. See here for more details. I don't foresee any way to customize QLPreviewController. The following answer remains for anyone interested for pre-iOS 6.

前几天我回答了一个几乎相同的问题此处。问题在于移除打印按钮,这不是太难。关于 QLPreviewController 的一点需要注意的是它不是要自定义的。我已经构建了一个可以自定义的 QLPreviewController 的子类。我在Github上把它这里。它的设计也可以轻松删除动作按钮等功能。用自定义按钮替换按钮不会花费太多精力。

I answered an almost identical question the other day here. The question pertained to removing the print button, which isn't too hard. One thing to note about QLPreviewController is that it's not meant to be customized. I have built a subclass of QLPreviewController that can be customized. I've put it here on Github. It's designed to easily remove the action button, among other features too. It wouldn't take much effort at all to replace the button with a custom one.

最值得注意的是,只要显示新文档,操作按钮就会重新添加到导航栏。您应该在我的代码中注意到这一点。随时 RBFilePreviewer 删除操作按钮,您只需重新添加自定义按钮即可。要添加自定义按钮,您应该创建一个 UIBarButtonItem ,其中包含一个自定义视图,其中包含四个按钮。然后将右侧栏按钮项设置为您创建的自定义 UIBarButtonItem

The biggest thing to watch out for is that the action button is re-added to the navigation bar anytime a new document is displayed. You should notice this in my code. Anytime RBFilePreviewer removes the action button, you just need to re-add your custom buttons. To add your custom buttons, you should create a UIBarButtonItem that holds a custom view with four buttons in it. Then set the right bar button item as the custom UIBarButtonItem you created.

更新:

我已更新 RBFilePreviewer 允许您开箱即用设置自定义右侧栏按钮项。只需在 RBFilePreviewer 上调用 -setRightBarButtonItem:,它就可以了。

I've updated RBFilePreviewer to allow you to set a custom right bar button item right out-of-the-box. Just call -setRightBarButtonItem: on RBFilePreviewer and it just works.

这篇关于QLPreviewController删除或添加UIBarButtonItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆