0

啊,我快疯了......我几个小时以来一直在尝试让我的 UIDocumentInteractionController 的 NavigationBar 不透明,但没有任何效果......

它显示为预览

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

然后我尝试从初始 ViewController(不是半透明的)分配 NavigationController:

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

那没用...... DocumentPreview 中的 NavigationBar 仍然是半透明的。

好的,所以我尝试操作 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;

}

同样在这里, NavigationBar 仍然是半透明的。接下来我尝试在 AppDelegate 中更改整个 App 的外观:

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

    [[UINavigationBar appearance] setTranslucent:NO];

}

那也不起作用……不,我没有任何进一步的想法可以做什么。我也在这里搜索了所有问答,但没有找到任何解决方案。

它是一个错误吗?或者你知道我该如何解决这个问题吗?

--- 我的解决方案 ---

由于我没有找到通用解决方案,因此我现在通过在 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;

}

不是最好的解决方案,但它有效......

4

1 回答 1

0

你可以给 ViewController 而不是 NavigationController。使用当前 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 ,在呈现的 UIDocumentInteractionController 上工作。

于 2018-02-01T09:11:17.770 回答