简体   繁体   English

带有半透明NavigationBar的UIDocumentInteractionController

[英]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 .. 啊,我快疯了……自数小时以来,我一直在努力使UIDocumentInteractionController的NavigationBar不透明,但没有任何效果..

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): 然后,我尝试从初始ViewController(不是半透明的)分配NavigationController:

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

That didn't work ... NavigationBar in DocumentPreview is still translucent. 那没有用。DocumentPreview中的NavigationBar仍然是半透明的。

OK, so I tried to manipulate the NavigationBar: OK,所以我尝试操纵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. 同样在这里,NavigationBar仍然是半透明的。 Next I tried to change the Appearance for the whole App in AppDelegate: 接下来,我尝试在AppDelegate中更改整个App的外观:

- (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: 由于找不到通用解决方案,因此我现在通过在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. 您可以给ViewController而不是NavigationController。 Using below code UIDocumentInteractionController present on current ViewController. 使用当前ViewController上存在的以下代码UIDocumentInteractionController。

- (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. setTranslucent = false,在显示的UIDocumentInteractionController上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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