Hide right button n QLPreviewController?

后端 未结 8 1439
-上瘾入骨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 01:54

    Go for subclassing the QLPreviewController and use the below-mentioned code, that only works fine for me using Xcode 8.3.

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            if let firstView = self.childViewControllers.first    {
                for view in firstView.view.subviews    {
                    if view.isKind(of: UIToolbar.self) {
                        view.removeFromSuperview()
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-17 01:58

    Simple solution for this is add one dummy view to current viewController and Add QLPreviewControlle.view to dummy view .

     previewController = [[QLPreviewController alloc] init];
     previewController.dataSource = self;
     previewController.delegate = self;
     previewController.currentPreviewItemIndex = 0;
    
    [self.ContentView addSubview:previewController.view];
    
    
    
    - (IBAction)removeQPView:(id)sender {
    
        [previewController.view removeFromSuperview];
    }
    
    0 讨论(0)
  • 2021-01-17 02:00

    I have found a solution to disable (not hide) therightBarButtonItem in QLPreviewController

    The solution works fine for me in iOS8 and iOS9

    You simply need to subclass QLPreviewController and override the following methods, then use your subclass instead of the original QLPreviewController

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // When coming back from background we make sure the share button on the rightbBarButtonItem is disabled
        __weak typeof(self) weakSelf = self;
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            weakSelf.navigationItem.rightBarButtonItem.enabled = NO;
        }];
    }
    
    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        self.navigationItem.rightBarButtonItem.enabled = NO; // Disable the share button
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        self.navigationItem.rightBarButtonItem.enabled = NO; // Disable the share button
    }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-17 02:16

    I'm dealing the same problem also.

    I made the rightBarButton hidden, but may have some problem when the loading of pdf lasts for a long time.

    Below is my process.

    1.Make a sub class of QLPreviewController.

    2.Add a timer to repeats setting the rightBarButton to nil when the class init.

    _hideRightBarButtonTimmer = [NSTimer scheduledTimerWithTimeInterval:0.01
                                                                     target:self
                                                                   selector:@selector(hideRightButton)
                                                                   userInfo:nil
                                                                    repeats:YES];
    

    3.Invalidate the timer in viewDidAppear.

    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(cancelTimmer) userInfo:nil repeats:NO];
    

    And I found the rightBarButton is setup while the loading of pdf file is finished. If we can detect the event the solution will be much easier and clearer.

    Hopes it will be helpful.

    0 讨论(0)
  • 2021-01-17 02:17

    Another way to achieve this is by subclassing UIToolbar and overriding setItems(_:animated:). You can return an empty array if you want to remove all or return only buttons you want to keep. Here is an example PreviewControllerHideBottomButtons

    0 讨论(0)
提交回复
热议问题