GoshDangitBobby
GoshDangitBobby

Reputation: 243

Is it possible to remove the navigation bar from QLPreviewController

I currently have a UIView that programmatically has an embedded QLPreviewController in it. I need to get rid of the default navigator bar that the QLPreviewController has when the document/url is loaded. Is there a way to do this?

Currently, I've tried subclassing QLPreviewController and in the viewDidAppear set self.navigationController!.navigationBarHidden = true. But this doesn't work.

Sorry if this is a dupe question - I've been looking online the last few days and couldn't find a concrete answer with iOS 8/9.

Upvotes: 4

Views: 2165

Answers (4)

MrAn3
MrAn3

Reputation: 1395

This worked for me:

class CustomQLPreview: QLPreviewController {
    ...        
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()    
        navigationController?.setNavigationBarHidden(true, animated: false)          
    }
}

Upvotes: 0

Eric Mentele
Eric Mentele

Reputation: 1060

It does appear to be possible. After inspecting the view hierarchy at runtime I found that the nav bar you see is actually a subview of the View Controller's view. The code below will remove it; however, it will not stay gone and it does not appear that there is any sanctioned way to modify the UI elements of this class. Any modification of this class will be a fragile hack and I'd recommend finding something less locked down to customize.

class MyPreviewViewController: QLPreviewController {
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let subviewsWithNav = self.view.subviews.first?.subviews {
            for view in subviewsWithNav {
                if let navbar = view as? UINavigationBar {
                    navbar.isHidden = true
                }
            }
        }
    }
}

Upvotes: 0

violethill
violethill

Reputation: 67

I Solve this problem by using addChildViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupPreviewController];
}

- (void)setupPreviewController {
    self.previewController = [[QLPreviewController alloc] init];
    [self addChildViewController:self.previewController];
    [self.view addSubview:self.previewController.view];

    //do autolayout
    [self.previewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    self.navigationController.navigationBarHidden = YES;
}

Upvotes: 3

Bhaskar Cm
Bhaskar Cm

Reputation: 38

Same thing apply in viewWillAppear and in viewDidLoad methods self.navigationController!.navigationBarHidden = true

i hope this will help

Upvotes: 0

Related Questions