我只需要在某些特定视图上更改导航栏的颜色。 有很多关于修改导航栏颜色的讨论,比如https://stackoverflow.com/a/18870519/938380,但它们都改变了同一导航层次结构下每个视图的导航栏颜色。
我想更改 特定视图 的颜色,并保持其他视图的颜色相同。我如何实现这一目标?
答案 0 :(得分:4)
Swift 4 使用
override func willMove(toParentViewController parent: UIViewController?) {
self.navigationController?.navigationBar.barTintColor = .red
}
它提供了更清晰的动画
答案 1 :(得分:1)
如果您使用标准导航栏,则无法执行此操作。但你可以使用一些作弊;)例如,你可以将这个代码(或类似的东西)添加到你的控制器:
- (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;
}
答案 2 :(得分:0)
您可以设置导航控制器的条形色调属性
我希望这个帮助你
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
答案 3 :(得分:0)
我通过对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