问题
I use QLPreviewController
in my app and want to hide bottom toolbar which allows to move through it's datasource items. Is it possible to do somehow?
I tried to search it as a subview
of QLPreviewController's view
but it has only one subview of _UISizeTrackingView
class . As i understand it's a private class so i have no rights to look for something there.
Are there any ways to hide this toolbar and does Apple allow to make that? Thank you.
回答1:
QLPreviewViewController
can have more than 1 toolbar inside. That's why you need to find all UIToolbar
in subviews and hide them.
Also you need observe change of hidden
property because when user tap in QLPreviewViewController
it change visibility of toolbars and navigation bars.
Swift 3:
var toolbars: [UIToolbar] = []
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
toolbars = findToolbarsInSubviews(forView: view)
for toolbar in toolbars {
toolbar.isHidden = true
toolbar.addObserver(self, forKeyPath: "hidden", options: .new, context: nil)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
for toolbar in toolbars {
toolbar.removeObserver(self, forKeyPath: "hidden")
}
}
private func findToolbarsInSubviews(forView view: UIView) -> [UIToolbar] {
var toolbars: [UIToolbar] = []
for subview in view.subviews {
if subview is UIToolbar {
toolbars.append(subview as! UIToolbar)
}
toolbars.append(contentsOf: findToolbarsInSubviews(forView: subview))
}
return toolbars
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let keyPath = keyPath,
let toolbar = object as? UIToolbar,
let value = change?[.newKey] as? Bool,
keyPath == "hidden" && value == false {
toolbar.isHidden = true
}
}
回答2:
After looking for a lot of answers,finally,I found the solution.If you want to hide the bottomToolBar all the time,you can do this by following the steps below.
- First subclass of QLPreviewController
Implement the "getToolBarFromView" function to get the toolBar of QLPreviewController
- (UIToolbar *)getToolBarFromView:(UIView *)view { // Find the QL ToolBar for (UIView *v in view.subviews) { if ([v isKindOfClass:[UIToolbar class]]) { return (UIToolbar *)v; } else { UIToolbar *toolBar = [self getToolBarFromView:v]; if (toolBar) { return toolBar; } } } return nil; }
Get the toolBar and add an observer to watch its 'Hidden' property.
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; // Get the ToolBar self.qlToolBar = [self getToolBarFromView:self.view]; self.qlToolBar.hidden = true; if (self.qlToolBar) { [self.qlToolBar addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionPrior context:nil]; } } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self.navigationController removeObserver:self forKeyPath:@"hidden"]; }
Implement - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context method
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ BOOL isToolBarHidden = self.qlToolBar.hidden; // If the ToolBar is not hidden if (!isToolBarHidden) { dispatch_async(dispatch_get_main_queue(), ^{ self.qlToolBar.hidden = true; }); } }
And the last thing you should do is to present this controller from your own navigationController.Hope it works for you !
回答3:
public override void ViewDidLayoutSubviews()
{
this.NavigationController.Toolbar.Hidden = true;
}
this will work for sure
回答4:
1.First subclass of QLPreviewController
2.coding
(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[UIToolbar class]]) {
obj.hidden = YES;
}
}];
[self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[UIToolbar class]]) {
obj.hidden = YES;
}
}];
}
回答5:
there is an easier solution, you have to change a QLPreviewControllerDataSource method.
func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
return 1
}
Setting the number of previewed items to 1 will automatically hide the bottom toolbar.
来源:https://stackoverflow.com/questions/15811834/qlpreviewcontroller-hide-bottom-toolbar