2012-06-12 34 views

Trả lời

66

này làm việc:

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil]; 
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions]; 
+5

Bạn nên chấp nhận câu trả lời của riêng bạn. –

+0

rất hữu ích .. cảm ơn –

+2

Hoạt động tốt. Nếu bạn cũng cần đặt chủ đề cho UINavigationBar đang hoạt động, hãy thêm dòng này: '[yourViewController.navigationController.navigationBar setTitleTextAttributes: textTitleOptions]' –

3

Đó treo ứng dụng trước khi UINavigationBar không có một tiêu đề hoặc tiểu bang ... Đó là những phương pháp UIButton

Bạn cần

[[UINavigationBar appearance] setTintColor:[UIColor darkGrayColor]]; 
+0

Không, điều đó không hiệu quả. Tôi nghĩ rằng đó là để nhuộm chính thanh đó. Chúng tôi đã sử dụng phương thức setBackgroundImage: để thay đổi nền thanh điều hướng, vì vậy bây giờ tôi cần thay đổi màu văn bản từ màu trắng sang màu xám đậm để dễ đọc. – RyJ

+0

Rất tiếc, không biết bạn đang cố đặt màu văn bản navBar. nghĩ rằng bạn đang cố gắng thay đổi màu sắc của nó. –

2

Các @ Câu trả lời của RyJ rất tuyệt vời và làm việc cho tôi. Nghĩ rằng tôi muốn chip trong đó có một hướng dẫn tốt về vấn đề này trong trang web Ray Wenderlich của, có tiêu đề (xin lỗi các chơi chữ):

User Interface Customization in iOS 6

Xem phần Tuỳ chỉnh UINavigationBar

Dưới đây là đoạn mã cho tiêu đề thanh điều hướng, để thay đổi trên toàn cầu:

// Customize the title text for *all* UINavigationBars 
[[UINavigationBar appearance] setTitleTextAttributes: 
[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
    UITextAttributeTextColor, 
    [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
    UITextAttributeTextShadowColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
    UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"Arial-Bold" size:0.0], 
    UITextAttributeFont, 
    nil]]; 

Một điểm nhỏ khác là có vẻ như có bóng mặc định trên thanh tiêu đề, để loại bỏ nó, bạn không thể chỉ xóa thuộc tính. Thay vào đó, bạn phải đặt độ lệch bóng:

UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0, 0)] 
0

Tôi đã sử dụng mã sau để thay đổi màu của thanh tiêu đề.

NSShadow *shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor blackColor]; 
shadow.shadowOffset = CGSizeMake(1, 0); 

NSDictionary *titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], 
              NSShadowAttributeName:shadow}; 

[[UINavigationBar appearance] setTitleTextAttributes:titleTextAttributes]; 
4

Dưới đây là một ví dụ về làm thế nào để làm điều này trong Swift:

UINavigationBar.appearance().titleTextAttributes = 
    [NSFontAttributeName:UIFont(name:"Exo2-Bold", size: 18) as! AnyObject, 
    NSForegroundColorAttributeName:UIColor.whiteColor()] 
0

Sử dụng cú pháp hiện đại và mã mà thực sự chạy, đây là làm thế nào để trên toàn cầu kiểu chữ UINavigationBar tiêu đề của bạn:

NSShadow *navigationBarTitleShadow = [[NSShadow alloc] init]; 
navigationBarTitleShadow.shadowColor = [UIColor colorWithWhite:0.5 
                 alpha:0.5]; 
navigationBarTitleShadow.shadowOffset = CGSizeMake(2.0, 2.0); 
[[UINavigationBar appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor], 
                 NSFontAttributeName : [UIFont fontWithName:@"Arial-BoldMT" 
                           size:30.0], 
                 NSShadowAttributeName : navigationBarTitleShadow }]; 

Lưu ý: NSShadow 's shadowBlurRadius tài sản không được tôn trọng.

Lưu ý: Bóng tối là iOS 6. Không bao giờ sử dụng chúng.

Các vấn đề liên quan