QLPreviewController hide bottom toolbar

≯℡__Kan透↙ 提交于 2019-12-04 17:09:59

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
    }
}

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 !

public override void ViewDidLayoutSubviews()
        {
            this.NavigationController.Toolbar.Hidden = true;

        }

this will work for sure

kop

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;
        }
    }];
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!