具有半透明NavigationBar的UIDocumentInteractionController

时间:2018-02-01 02:29:35

标签: ios objective-c uinavigationbar uidocumentinteractioncontroller

啊,我疯了......我正在努力让我的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中更改整个应用程序的外观:

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

    [[UINavigationBar appearance] setTranslucent:NO];

}

那也不起作用......不,我没有任何进一步的想法,我能做什么。我也在这里搜索了所有Q& A,但没有找到任何解决方案。

这是一个错误吗?或者你知道我怎么能解决这个问题吗?

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

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

}

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

1 个答案:

答案 0 :(得分:0)

您可以提供ViewController而不是NavigationController。使用下面的代码UIDocumentInteractionController出现在当前的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,处理呈现的UIDocumentInteractionController。