如何更改特定视图上导航栏的颜色
Posted
技术标签:
【中文标题】如何更改特定视图上导航栏的颜色【英文标题】:How to change the color of navigation bar on a specific view 【发布时间】:2015-03-02 10:42:35 【问题描述】:我只需要在某些特定视图上更改导航栏的颜色。 有很多关于修改导航栏颜色的讨论,比如https://***.com/a/18870519/938380,但都是在同一个导航层级下改变每个视图的导航栏颜色。
我想更改特定视图的颜色并保持其他视图的颜色相同。我如何做到这一点?
【问题讨论】:
【参考方案1】:对于 Swift 4 使用
override func willMove(toParentViewController parent: UIViewController?)
self.navigationController?.navigationBar.barTintColor = .red
它提供了更清晰的动画
【讨论】:
【参考方案2】:如果您使用标准导航栏,则无法做到。但是您可以使用一些作弊方法;)例如,您可以将此代码(或类似的东西)添加到您的控制器中:
- (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;
【讨论】:
【参考方案3】:可以设置navigationController的Bar Tint属性
希望这篇文章对你有帮助
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
【讨论】:
【参考方案4】:我通过 UIViewController 的扩展来处理这种事情。就我而言,我想让选定的导航栏透明。你可以扩展它来处理颜色。这样可以节省在多个控制器中粘贴相同的代码。代码如下。
您从viewWillAppear
致电[self makeNavigationBarTransparent]
。从viewWillDisappear
你打电话给[self restoreNavigationBar]
对于您的情况,您只需将其扩展为添加 makeNavigationBarColored
。
一个选项是子类 UINavigationController 来创建一个具有特定颜色的类。在它的实现中使用这个扩展来设置颜色。在您的故事板中,使您希望此颜色成为您的新类类型的任何控制器。然后,您将在情节提要中完成所有这些操作。
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
【讨论】:
以上是关于如何更改特定视图上导航栏的颜色的主要内容,如果未能解决你的问题,请参考以下文章