2012-06-02 53 views
6

Tôi tiếp tục đọc để thay đổi màu sắc mặc định trên một Navigationbar tôi chỉ cần cập nhật các phương pháp đầu tiên trong appdelegate nàyThay đổi Navigationbar màu (màu nền)

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

nhưng nó dường như không làm việc, vì vậy tôi đã thử cài đặt nó trong phương thức viewDidLoad của chế độ xem trước cũng:

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

Điều này cũng không có tác dụng. Làm thế nào tôi có thể thay đổi điều này?

Trả lời

0

Hãy thử self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIViewController lớp có một tài sản navigationController, mà trả về điều khiển chuyển hướng của nó được nhúng vào bất kể có bao sâu, nếu không nó sẽ trả về nil.

6

Không sử dụng self.parentViewController, nhưng self:

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

Khi iphone 5 đến chúng ta phải đặt cả loại thiết bị. Vì vậy, sử dụng này

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

Tôi thích sử dụng UIImage hơn màu tô màu. Cảm ơn. – Flea

1

Trong iOS7 bạn có thể thử này:

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

Bạn có thể làm theo các bước sau:

Tôi tạo ra một mới UINavigationController ví dụ UIDemoNavController kết quả là:

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

Đây là lớp demo đầy đủ:

#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 
Các vấn đề liên quan