我知道這是一個古老的問題,但我花了很多小時尋找解決方案,並提出了一些有用的東西。
因此,對於尋找與我相同的東西的任何人。這是我的解決方案。
代碼是在Objective-C,但it'l是一個簡單的轉換斯威夫特
首先,我們創建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。
也許這些可以幫助:http://stackoverflow.com/questions/22953117/hide-right-button-n-qlpreviewcontroller? – Terry
適合我工作https://stackoverflow.com/a/45344701/1603380 – Buntylm