QLPreviewController NavigationBar setTranslucent property not working
Asked Answered
S

4

7

i have set the custom color to navigation bar of QLPreviewController but the problem is that i want dark color of navigation bar in QLPreviewController even i have set the navigation bar translucent property to No But i Dont know Why it is not working

I want it like belowed image

enter image description here

but it shows like this image

enter image description here

QLPreviewController *previewer = [[QLPreviewController alloc] init];
// Set data source
[previewer setDataSource:self];
[previewer setDelegate:self];
// Which item to preview
[previewer setCurrentPreviewItemIndex:index];
[previewer.view addGestureRecognizer:singleTap];
previewer.navigationController.navigationBar.translucent = NO;
previewer.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent=NO;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
   [self.navigationController pushViewController:previewer animated:TRUE ];

even i have also tried like this but it is also not working

    - (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// Break the path into it's components (filename and extension)
// Use the filename (index 0) and the extension (index 1) to get path
//lblFileName.text=[strFileName stringByReplacingOccurrencesOfString:@"movefile" withString:@""];

// For navigation bar color and text attributes of navigation bar
for (id object in controller.childViewControllers)
{
    if ([object isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = object;
        navController.navigationBar.translucent=NO;
        navController.navigationBar.barTintColor = [UIColor redColor];;
        navController.toolbar.translucent=NO;
        navController.toolbar.barTintColor=[UIColor redColor];;
        [navController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];


    }
}

   NSString *strFilename=[[NSBundle mainBundle]pathForResource:@"final" ofType:@"png"];

return [NSURL fileURLWithPath:strFilename];
}

Please suggest me where i am going wrong Thanks In Advance

Silversmith answered 16/11, 2013 at 5:45 Comment(0)
N
10

The main problem is that when you try to set the translucency of the navigation bar, you haven't pushed the preview controller on the navigation stack yet. At this point, the preview controller is allocated and instantiated, but its view has not been loaded or added to the view hierarchy, and the value of previewer.navigationController is nil. The value of self.navigationController is not nil at this point, but the translucency property you set here will be overwritten as a side effect of pushing the preview controller. The easiest way to get the effect you want, is to swap the order of the statements, like this:

[self.navigationController pushViewController:previewer animated:YES];
self.navigationController.navigationBar.translucent = NO;

Do note that with the translucency of the navigation bar set to NO, the previewed content will start under the navigation bar, which is probably not what you want. The easiest way to work around that issue, is to set the translucency property after the view of the preview controller has appeared on screen. You can do this by subclassing QLPreviewController:

@interface PreviewController : QLPreviewController

@end

@implementation PreviewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.navigationController.navigationBar.translucent = NO;
}

Note that things get more complicated when you present the preview controller modally (rather than pushing it on a navigation stack). In that case, there is no navigation controller available to access the navigation bar and you need to rely on the internal view hierarchy of the QLPreviewController. The following code works in iOS7, but might break in a later version:

[self presentViewController:previewController animated:YES completion:^{

    UIView *view = [[[previewController.view.subviews lastObject] subviews] lastObject];
    if ([view isKindOfClass:[UINavigationBar class]])
    {
        ((UINavigationBar *)view).translucent = NO;
    }

}];
Neutrino answered 10/2, 2014 at 19:13 Comment(0)
J
2

No need for subclassing. try this:

 QLPreviewController * ql = [[QLPreviewController alloc] init];
...
[self showViewController:ql sender:self];
ql.navigationController.navigationBar.translucent = NO;
ql.edgesForExtendedLayout = UIRectEdgeNone;

If you want to present it modally, just put the previewController into a navigationController:

UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:ql];
        ql.navigationController.navigationBar.translucent = NO;
        ql.edgesForExtendedLayout = UIRectEdgeNone;

        [self presentViewController:navi animated:YES completion:nil];
Jempty answered 14/9, 2015 at 7:37 Comment(0)
I
0

Instead of using QLPreviewController,y don't you use webview to load the pdf.But if you load pdf in webview you need to customise the buttons thats it.Its also look very similar to QLPreviewController.

 NSURL *targetURL = [NSURL fileURLWithPath:path_pdf]; //path_pdf is pdf's path 
 NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
 [webview_pdf loadRequest:request];

Well,try it once.

Idiotic answered 14/2, 2014 at 9:20 Comment(2)
WebView do not support sharing option, So it is not used/preferred.Viewpoint
As of iOS 10, both UIWebView and WKWebView have memory leak issues, in which the memory assigned to the decompressed PDF is leaked. IMO this makes it unusable.Thaler
D
0

If Still, anyone wants to customize the Navigation bar of QLPreviewController then they can try creating a UIViewController and customize it as per requirement and then create QLPreviewController object and add it as a child view controller.

This will allow you to get rid of share button and you customize navigation bar color etc. this is working for me.

To know, how to add child view controller you can refer to this

Doubletalk answered 7/11, 2018 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.