如何更改特定视图上导航栏的颜色

How to change the color of navigation bar on a specific view

我只需要在某些特定视图上更改导航栏的颜色。 关于修改导航栏颜色的讨论有很多,比如,但他们都在同一导航层次结构下的每个视图上更改导航栏的颜色。

我想更改 特定视图 的颜色并使其他视图保持相同颜色。我该如何实现?

您可以设置栏色调 属性 OF navigationController

希望本文对您有所帮助

[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];

如果你使用标准的导航栏,你就做不到。但是你可以使用一些作弊 ;) 例如,你可以将这段代码(或类似的东西)添加到你的控制器中:

 - (void)viewDidAppear:(BOOL)animated {
     [super viewDidAppear:animated];
     oldColor = self.navigationController.navigationBar.backgroundColor;//probably barTintColor instead of backgroundColor
     self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
 }
 - (void)viewWillDisappear:(BOOL)animated {
     [super viewWillDisappear:animated];
     self.navigationController.navigationBar.backgroundColor = oldColor;
 }

我通过对 UIViewController 的扩展来处理这种事情。在我的例子中,我想让选定的导航栏透明。您可以扩展它来处理颜色。这节省了在多个控制器中粘贴相同的代码。下面的代码。

您从 viewWillAppear 呼叫 [self makeNavigationBarTransparent]。从 viewWillDisappear 你打电话给 [self restoreNavigationBar]

对于您的情况,您只需扩展它以添加 makeNavigationBarColored.

一个选项是还子 class UINavigationController 以创建具有特定颜色的 class。在其实现中使用此扩展来设置颜色。在你的故事板中,让任何你想要这种颜色的控制器成为你的新 class 类型。然后你正在从情节提要中完成所有这些。

UIViewController+TransparentNavigationBar.h

#import <UIKit/UIKit.h>

@interface UIViewController (TransparentNavigationBar)

/** Makes the current navigation bar transparent and returns a context holding
 * the original settings.
 */
- (void) makeNavigationBarTransparent;

/**
 * Restores the current navigation bar to its original settings.
 */
- (void) restoreNavigationBar;

@end

UIViewController+TransparentNavigationController.m

#import "UIViewController+TransparentNavigationBar.h"
#import <objc/runtime.h>

@interface VSSNavigationBarContext:NSObject

/** Backup of nav bar image used to restore on push. */
@property UIImage *originalNavBarBackgroundImage;
/** Backup of nav bar shadow image used to restore on push. */
@property UIImage *originalNavBarShadowImage;
/** Backup of nav bar color used to restore on push. */
@property UIColor *originalNavBarColour;

@end

@implementation VSSNavigationBarContext

- (id) init {
    self=[super init];
    if (self){
        self.originalNavBarBackgroundImage=nil;
        self.originalNavBarShadowImage=nil;
        self.originalNavBarColour=nil;
    }
    return self;
}

@end

static char const * const ObjectTagKey = "NavBarContextTag";

@implementation UIViewController (TransparentNavigationBar)

- (VSSNavigationBarContext *) getContext
{
    VSSNavigationBarContext *context=(VSSNavigationBarContext *)objc_getAssociatedObject(self, &ObjectTagKey);
    return context;
}

- (void) makeNavigationBarTransparent{
    VSSNavigationBarContext *context=(VSSNavigationBarContext *)objc_getAssociatedObject(self, &ObjectTagKey);
    if (context == nil){
        context=[[VSSNavigationBarContext alloc] init];

        context.originalNavBarBackgroundImage=[self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
        context.originalNavBarShadowImage=self.navigationController.navigationBar.shadowImage;
        context.originalNavBarColour=self.navigationController.view.backgroundColor;

        // Store the original settings
        objc_setAssociatedObject(self, &ObjectTagKey, context, OBJC_ASSOCIATION_RETAIN);
    }

    //
    // Make transparent
    //
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f){
        self.navigationController.navigationBar.translucent = YES;
    }
    else{
        self.navigationController.navigationBar.translucent = NO;
    }
    self.navigationController.view.backgroundColor = [UIColor clearColor];
}

- (void) restoreNavigationBar
{
    VSSNavigationBarContext *context=(VSSNavigationBarContext *)objc_getAssociatedObject(self, &ObjectTagKey);
    if (context != nil){
        // Restore original
        [self.navigationController.navigationBar setBackgroundImage:context.originalNavBarBackgroundImage forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = context.originalNavBarShadowImage;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f){
            self.navigationController.navigationBar.translucent = YES;
        }
        else{
            self.navigationController.navigationBar.translucent = NO;
        }
        self.navigationController.view.backgroundColor = context.originalNavBarColour;
    }
}

@end

对于Swift 4使用

override func willMove(toParentViewController parent: UIViewController?) {
    self.navigationController?.navigationBar.barTintColor = .red
}

它提供了更清晰的动画