簡體   English   中英

帶有半透明NavigationBar的UIDocumentInteractionController

[英]UIDocumentInteractionController with translucent NavigationBar

啊,我快瘋了……自數小時以來,我一直在努力使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仍然是半透明的。

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;

}

同樣在這里,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;

}

不是最好的解決方案,但它可以工作...

您可以給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上工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM