繁体   English   中英

更改NavigationBar颜色(背景颜色)

[英]Change NavigationBar color (background color)

我一直在阅读以更改导航栏上的默认颜色,我只需要将appdelegate中的第一个方法更新为此

self.window.rootViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];

但它似乎没有用,所以我尝试在firstview的viewDidLoad方法中设置它:

self.parentViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];

这也不起作用。 我怎么能改变这个?

不要使用self.parentViewController ,而是self

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

当iphone 5到来时,我们必须设置两种设备类型。 所以使用它

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    //iOS 5 new UINavigationBar custom background
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
    [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}

在IOS7中你可以试试这个:

[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

您可以按照以下步骤操作:

我创建了一个新的UINavigationController ,例如UIDemoNavController导致:

- (void)viewDidLoad{
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];  
    [super viewDidLoad];
}

这是完整的演示类:

#import "UIDemoNavController.h"

@interface UIDemoNavController()

@end

@implementation UIDemoNavController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {}
    return self;
}

- (void)viewDidLoad{
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

@end

试试self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIViewController类有一个属性navigationController ,它返回嵌入的导航控制器,无论多深,否则返回nil

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM