隐藏右键n QLPreviewController?
我在我的应用程序中inheritanceQLPreviewController并使用下面的代码。
QLPreviewControllerSubClass* preview = [[QLPreviewControllerSubClass alloc] init]; [self presentViewController:preview animated:YES completion:^() { // do more stuff here }];
我想隐藏正确的酒吧button
-(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self navigationItem].rightBarButtonItems = nil; }
但它不隐藏。任何帮助将是可观的
我也处理同样的问题。
我做了右边的BarButton,但是当pdf的加载时间很长时,可能会有一些问题。
以下是我的过程。
1.创build一个QLPreviewController的子类。
2.添加一个定时器来重复设置rightBarButton,当init初始化时为零。
_hideRightBarButtonTimmer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(hideRightButton) userInfo:nil repeats:YES];
3.使viewDidAppear中的定时器无效。
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(cancelTimmer) userInfo:nil repeats:NO];
我发现右边的BarButton是在pdf文件加载完成的时候设置的。 如果我们能够发现事件,解决scheme将变得更加容易和清晰。
希望这会有所帮助。
我find了一个解决scheme来禁用( 不隐藏 )在QLPreviewController
中的QLPreviewController
该解决scheme适用于iOS8和iOS9
您只需要QLPreviewController
并覆盖以下方法,然后使用您的子类而不是原始的QLPreviewController
- (void)viewDidLoad { [super viewDidLoad]; // When coming back from background we make sure the share button on the rightbBarButtonItem is disabled __weak typeof(self) weakSelf = self; [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) { weakSelf.navigationItem.rightBarButtonItem.enabled = NO; }]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.rightBarButtonItem.enabled = NO; // Disable the share button } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.navigationItem.rightBarButtonItem.enabled = NO; // Disable the share button }
使用swift在iOS 9上进行testing。
最近不得不解决这个问题,并无法find隐藏此dangbutton的方法。 我终于设法隐藏正确的共享button使用从这个stackoverflowpost的答案。
基本上,你想要子类化QLPreviewController,并在viewWillAppear()函数中调用inspectSubviewForView()函数。 一旦find包含分享button的导航项目,您可以将其删除:
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) // ** traverse subviews until we find the share button ** // inspectSubviewForView(self.view) } func inspectSubviewForView(view: UIView) { for subview in view.subviews { if subview is UINavigationBar { // ** Found a Nav bar, check for navigation items. ** // let bar = subview as! UINavigationBar if bar.items?.count > 0 { if let navItem = bar.items?[0] { // ** Found the share button, hide it! ** // hideRightBarItem(navItem) } } } if subview.subviews.count > 0 { // ** this subview has more subviews! Inspect them! ** // inspectSubviewForView(subview) } } } func hideRightBarItem(navigationItem: UINavigationItem) { // ** Hide/Remove the Share button ** // navigationItem.setRightBarButtonItem(nil, animated: false) }
上述链接的上一张海报警告说,由于您正在访问私有API,因此可能无法通过苹果的审核stream程,因此请自担风险! 另外,如果Apple在更高版本的iOS中更新QLPreviewController,则此代码可能不再有效。
不过,这是唯一对我有用的解决scheme。 我希望它也适用于你!
感谢马修Kostelecky,我可以隐藏分享button,但我想添加一些细节为那些需要这个在多个文件的通用应用程序。
首先,马修的答案如果你使用QLPreviewController模态。 如果你从你的导航控制器像这样推QLPreviewController;
navigationController?.pushViewController(quickLookController, animated: true)
它不会能够find任何NavigationBar或工具栏。 你应该像这样模态地调用QLPreviewController;
presentViewController(quickLookController, animated: true, completion: nil)
另外,如果您正在开发通用应用程序,并且您有要播放的文件列表。 会有另一个button(List Button);
在iPhones中,如果你有多个文件,QLPreviewController将创build工具栏来显示“列表button”和“分享button”,这两个都将在工具栏上。
在iPad中,这两个button都在NavigationBar上,没有ToolBar。
所以除了马修的回答,你应该search工具栏,如果你有iphone中的多个文件;
func inspectSubviewForView(view: UIView) { for subview in view.subviews { if subview is UINavigationBar { // ** Found a Nav bar, check for navigation items. ** // let bar = subview as! UINavigationBar if bar.items?.count > 0 { if let navItem = bar.items?[0] { navItem.setRightBarButtonItem(nil, animated: false) } } } if subview is UIToolbar { // ** Found a Tool bar, check for ToolBar items. ** // let bar = subview as! UIToolbar if bar.items?.count > 0 { if let toolBarItem = bar.items?[0] { toolBarItem.enabled = false } } } if subview.subviews.count > 0 { // ** this subview has more subviews! Inspect them! ** // inspectSubviewForView(subview) } } }
这段代码将隐藏iPad上的分享button,并禁用iPhone上的分享button。
希望对那些仍然需要它的人有帮助。
简单的解决scheme是添加一个虚拟视图到当前viewController和添加QLPreviewControlle.view
到虚拟视图。
previewController = [[QLPreviewController alloc] init]; previewController.dataSource = self; previewController.delegate = self; previewController.currentPreviewItemIndex = 0; [self.ContentView addSubview:previewController.view]; - (IBAction)removeQPView:(id)sender { [previewController.view removeFromSuperview]; }
另一种实现方法是通过setItems(_:animated:)
UIToolbar并重写setItems(_:animated:)
。 如果要删除所有或仅返回您想保留的button,则可以返回一个空数组。 这里是一个PreviewControllerHideBottomButtons的例子
去QLPreviewController
子类,并使用下面提到的代码,这只适用于我使用Xcode 8.3
。
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) if let firstView = self.childViewControllers.first { for view in firstView.view.subviews { if view.isKind(of: UIToolbar.self) { view.removeFromSuperview() } } } }