简体   繁体   中英

UIDocumentInteractionController with translucent NavigationBar

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 ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM