How to hide share button in QLPreviewController using swift?

前端 未结 7 2163
无人共我
无人共我 2021-01-06 02:40

I\'m using the below code to use QLPreviewcontroller to show some documents in my app,

let ql = QLPreviewController()
ql.dataSource = self
//ql.navigationIte         


        
7条回答
  •  Happy的楠姐
    2021-01-06 02:59

    I know this is an old question but I spend so many hours looking for a solution and came up with something that works.

    So, for any one looking for the same thing as me. Here's my solution.

    Codes are in objective-c but it'l be a simple conversion to Swift

    First we create subclass of QLPreviewController and in the subclass override the following methods

    Edit

    Swift:

    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)
    

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

    Remove the Observer on viewWillDisappear

    -(void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[self.navigationController toolbar] removeObserver:self forKeyPath:@"hidden"];
    }
    

    And the observer method: Required because when you single tap the image to hide the navigation bar and toolbar, the share button becomes visible again on tap.

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

    And the PreviewController has to be pushed from you existing navigationController

    [self.navigationController pushViewController:qlPreviewController animated:YES];
    

    And also we have to use the subclass instead of QLPreviewController.

提交回复
热议问题