QLPreviewController hide bottom toolbar
Asked Answered
N

5

4

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.

Nutmeg answered 4/4, 2013 at 12:54 Comment(0)
P
5

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
    }
}
Preferable answered 21/11, 2016 at 19:49 Comment(1)
iOS 10.3 share button still show left botton corner.Delighted
E
1

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.

  1. First subclass of QLPreviewController
  2. 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;
    }
    
  3. 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"];
    }
    
  4. 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 !

Enclave answered 30/3, 2016 at 9:27 Comment(0)
H
-1
public override void ViewDidLayoutSubviews()
        {
            this.NavigationController.Toolbar.Hidden = true;

        }

this will work for sure

Hardback answered 17/3, 2017 at 16:38 Comment(0)
C
-1

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;
        }
    }];
}
Coreencorel answered 7/9, 2017 at 8:49 Comment(0)
D
-4

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.

Daylong answered 23/5, 2016 at 14:20 Comment(1)
This doesn't seem to work in iOS 10. The default appearance is to show the navigation bar and toolbar.Burushaski

© 2022 - 2024 — McMap. All rights reserved.