我知道这是一个老问题,但我花了很多时间寻找解决方案并想出了一些可行的方法。
因此,对于任何与我寻找相同事物的人。这是我的解决方案。
代码在 Objective-c 中,但它会是一个简单的 Swift 转换
首先我们创建 QLPreviewController 的子类,并在子类中覆盖以下方法
编辑
迅速:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.rightBarButtonItem = nil
//For ipads the share button becomes a rightBarButtonItem
self.navigationController?.toolbar?.isHidden = true
//This hides the share item
self.navigationController?.toolbar?.addObserver(self, forKeyPath: "hidden", options: NSKeyValueObservingOptionPrior, context: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.toolbar?.removeObserver(self, forKeyPath: "hidden")
}
override func observeValue(forKeyPath keyPath: String, ofObject object: Any, change: [AnyHashable: Any], context: UnsafeMutableRawPointer) {
var isToolBarHidden: Bool? = self.navigationController?.toolbar?.isHidden
// If the ToolBar is not hidden
if isToolBarHidden == nil {
DispatchQueue.main.async(execute: {() -> Void in
self.navigationController?.toolbar?.isHidden = true
})
}
}
self.navigationController?.pushViewController(qlPreviewController, animated: true)
目标-C:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationItem.rightBarButtonItem = nil; //For ipads the share button becomes a rightBarButtonItem
[[self.navigationController toolbar] setHidden:YES]; //This hides the share item
[[self.navigationController toolbar] addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionPrior context:nil];
}
删除 viewWillDisappear 上的观察者
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[self.navigationController toolbar] removeObserver:self forKeyPath:@"hidden"];
}
观察者方法:需要,因为当您单击图像以隐藏导航栏和工具栏时,共享按钮在点击时再次可见。
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
BOOL isToolBarHidden = [self.navigationController toolbar].hidden;
// If the ToolBar is not hidden
if (!isToolBarHidden) {
dispatch_async(dispatch_get_main_queue(), ^{
[[self.navigationController toolbar] setHidden:YES];
});
}
}
并且 PreviewController 必须从您现有的 navigationController 中推送
[self.navigationController pushViewController:qlPreviewController animated:YES];
而且我们还必须使用子类而不是 QLPreviewController。