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

查看:28
本文介绍了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.

编辑(解决方案):我不久前找到了解决方案,但没有更新这篇文章,所以这是我解决问题的方法:

我手动添加了所有按钮:

I add al the buttons manually:

// 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.Quick Look 在另一个使用 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天全站免登陆