簡體   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