7
votes

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

4

4 Answers

10
votes

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

}];
2
votes

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];
0
votes

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.

0
votes

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