Hide right button n QLPreviewController?

后端 未结 8 1706
-上瘾入骨i
-上瘾入骨i 2021-01-17 01:35

I am subclassing QLPreviewController in my application and using the following code.

QLPreviewControllerSubClass* preview = [[QLPreviewControllerSubClass all         


        
8条回答
  •  轮回少年
    2021-01-17 02:08

    Tested on iOS 9 with swift.

    Recently had to solve this issue and had trouble finding a way to hide this dang button. I finally managed to hide the right share button using an answer from this stackoverflow post.

    Basically, you want to subclass QLPreviewController and call the inspectSubviewForView() function in your viewWillAppear() function. Once you find the navigation item containing the share button, you can remove it:

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    
        // ** traverse subviews until we find the share button ** //
        inspectSubviewForView(self.view)
    }
    
    func inspectSubviewForView(view: UIView) {
        for subview in view.subviews {
            if subview is UINavigationBar {
                // ** Found a Nav bar, check for navigation items. ** //
                let bar = subview as! UINavigationBar
    
                if bar.items?.count > 0 {
                    if let navItem = bar.items?[0] {
                        // ** Found the share button, hide it! ** //
                        hideRightBarItem(navItem)
                    }
                }
            }
            if subview.subviews.count > 0 {
                // ** this subview has more subviews! Inspect them! ** //
                inspectSubviewForView(subview)
            }
        }
    }
    
    func hideRightBarItem(navigationItem: UINavigationItem) {
        // ** Hide/Remove the Share button ** //
        navigationItem.setRightBarButtonItem(nil, animated: false)
    }
    

    The previous poster from the above link warned that this may not get through apple's review process as you are accessing private APIs, so use at your own risk! Also if Apple updates the QLPreviewController in later versions of iOS this code may no longer work.

    Still, this is the only solution that has worked for me. I hope it works for you too!

提交回复
热议问题