2015-03-02 54 views

回答

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; 
} 
+0

非常甜蜜,非常感谢! – Cesare 2017-03-08 20:02:06

0

可以设置酒吧色调物业navigationController

我希望这个帮你

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

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