0

Ahh, I'm driving crazy ... I'm trying since hours to get the NavigationBar of my UIDocumentInteractionController to be NOT translucent, but nothing works ..

It is presented as Preview

_docController = [UIDocumentInteractionController new];
_docController.delegate = self;
[_docController setURL:[NSURL fileURLWithPath:_attachmentPath]];
[_docController presentPreviewAnimated:YES];

Then I tried to assign the NavigationController from the initial ViewController (which is not translucent):

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
    return [self navigationController];
}

That didn't work ... NavigationBar in DocumentPreview is still translucent.

OK, so I tried to manipulate the NavigationBar:

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {

    UINavigationController *nc = [self navigationController];

    [nc.navigationBar setAlpha:1.0];
    [nc.navigationBar setTranslucent:NO];
    [nc.navigationBar setOpaque:NO];
    [nc.navigationBar setBarStyle:UIBarStyleBlack];

    return nc;

}

Same here, NavigationBar is still translucent. Next I tried to change the Appearance for the whole App in AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [[UINavigationBar appearance] setTranslucent:NO];

}

That didn't work either ... no I don't have any further ideas what I can do. I have also searched through all Q&A here and didn't found any solution.

Is it a bug? Or do you know how I could solve this problem?

--- My Solution ---

As I did not find a generic solution, I now ended up with a workaround by adding a black subview under the NavigationBar:

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {

    UINavigationController *nc = [self navigationController];

    CGFloat navbarHeight = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;

    CGRect xFrame = CGRectMake(0.0f,
                               0.0f,
                               self.view.frame.size.width,
                               navbarHeight);

    UIView *blackView = [[UIView alloc] initWithFrame:xFrame];
    blackView.backgroundColor = [UIColor blackColor];

    [nc.view insertSubview:blackView atIndex:1];

    return nc;

}

Not the best solution, but it works ...

m.steini
  • 21
  • 2

1 Answers1

0

You can give ViewController instead of NavigationController. Using below code UIDocumentInteractionController present on current ViewController.

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    [[UINavigationBar appearance] setTintColor:self.navigationController.navigationBar.tintColor];
    [[UINavigationBar appearance] setBarTintColor:self.navigationController.navigationBar.barTintColor];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
    [[UINavigationBar appearance] setBackgroundImage:[self.class imageFromColor:self.navigationController.navigationBar.barTintColor] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTranslucent:false];
    return  self;
}
+ (UIImage *)imageFromColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

setTranslucent = false , work on presented UIDocumentInteractionController.

Datt Patel
  • 1,203
  • 1
  • 11
  • 10
  • Thank you for your answer. I tried your solution and indeed the NavigationBar is no more translucent, but it leads to other problems: The DocumentViewController does no longer respect the supportedInterfaceOrientations that I have set up before (should only be Portrait) and the Document starts with a top margin. I have now found a workaround by adding a black subview over the document and under the NavigationBar. – m.steini Feb 01 '18 at 13:19