2012-07-20 75 views

Trả lời

33

Tôi tin barButtonItem là read-only. (Tôi không quá chắc chắn về điều này, một người nào đó đúng cho tôi nếu tôi sai)

Để cung cấp cho một màu sắc để các nút này, đây là những gì tôi sẽ làm gì:

Trong App ủy quyền của bạn, thêm những dòng mã:

UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance]; 
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour 

dòng đầu tiên đặt một proxy xuất hiện, vì vậy tôi tin rằng công trình này cũng vậy, nếu bạn muốn mã ít hơn:

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]]; 

tôi hy vọng công trình này dành cho bạn! (Điều này thay đổi tất cả các trường hợp của UIBarButtonItem)

+2

Đó là những gì tôi đã làm, nhưng cùng một bộ điều khiển xem có thể kéo lên một bộ điều khiển xem Composer Mail, và nó thay đổi màu sắc của các nút ở đó, đi ngược lại không có quy tắc tùy chỉnh cho bộ điều khiển đó. – user717452

+0

người đàn ông tuyệt vời ... cảm ơn rất nhiều. – harshitgupta

29

Bạn không thể thay đổi màu sắc màu của UINavigationItem trực tiếp. Bạn phải thay đổi màu sắc của thanh điều hướng và nút thanh của bạn sẽ tự động thay đổi màu sắc của nó.

Thêm những dòng này trong phương pháp viewWillAppear bạn

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

Hoặc Bạn có thể tạo một nút tùy chỉnh và khởi UIBarButtonItem của bạn như

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton]; 
+5

Đây phải là câu trả lời được chấp nhận, IMO. – baptzmoffire

+3

tính năng này hoạt động trong 7! Cảm ơn nhiều! –

+2

Yeeeeep! Làm việc với iOS 7, Cảm ơn! : D – VICTORGS3

0

Vâng, bạn chắc chắn có thể thay đổi màu của chỉ nút quay lại hoặc có lẽ bất kỳ Nút Bar nào trên thanh Điều hướng. Đây là một đoạn nhỏ để làm điều đó.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil]; 
backButton.tintColor = [UIColor blackColor]; 
[self.navigationItem setBackBarButtonItem:backButton]; 
[backButton release]; 

Đây chỉ là một trong những cách để thực hiện việc này. Tôi hy vọng điều này sẽ giúp !! Tôi biết câu trả lời cho nó đã được chấp nhận, nhưng tôi nghĩ đưa ra một đoạn thích hợp cho nó. Chúc mừng !!! Happy Coding !!

0

Nếu bạn muốn đặt nó là phong cách được xác định trước, bạn có thể sử dụng

[navigationBar setBarStyle: UIBarStyleBlack]; 
0

Đặt đoạn mã sau vào appdelegate rằng sẽ thiết lập màu cho nút Back trên toàn cầu

//Set color of BackButtonItem globally 

[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:5.0/255.0 
green:127.0/255.0 blue:173.0/255.0 alpha:1.0]]; 

này được hỗ trợ trong iOS 5 và trên

23

Điều này phù hợp với tôi.

[self.navigationController.navigationBar setTintColor:[UIColor redColor]]; 

Nó rất giống với câu trả lời thứ hai ...

+1

Làm việc tốt cho tôi! –

+0

Tôi phải sử dụng giải pháp này vì giải pháp gợi ý '[[self.navigationController navigationBar] tintColor] = [UIColor myColor];' không được gán. –

0

Nó luôn luôn làm việc cho tôi:

  1. self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];

  2. GET DEFAULT LẠI NÚT

-

(UIBarButtonItem*) logicToAddBackButton 
     { 
      UIImageView *imageView; 

      // [imageView setTintColor:[UIColor redColor]]; 
      UILabel *label=[[UILabel alloc] init]; 
      if (WHITEBACK) { 
       imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBackIPAD"]]; 
       [label setTextColor:[UIColor whiteColor]]; 
      }else{ //DEFAULTBACK 
       imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBack"]]; 
       [label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]]; 

      } 

      [label setText:@"Back"]; 
      [label sizeToFit]; 

      int space=6; 
      label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, 
    label.frame.origin.y, label.frame.size.width, 
    label.frame.size.height); 
      UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space, 
    imageView.frame.size.height)]; 

      view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width, 
    view.bounds.size.height); 


      UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(view.bounds.origin.x, view.bounds.origin.y, 
    view.bounds.size.width, view.bounds.size.height)]; 
      button.bounds=CGRectMake(view.bounds.origin.x, view.bounds.origin.y, view.bounds.size.width, 
    view.bounds.size.height); 
      [button addTarget:self action:@selector(eventBack) forControlEvents:UIControlEventTouchUpInside]; 
      [button addSubview:imageView]; 
      [button addSubview:label]; 


      [UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ 
       label.alpha = 0.0; 
       CGRect orig=label.frame; 
       label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y -5, label.frame.size.width, 
    label.frame.size.height+10); 
       label.alpha = 1.0; 
       label.frame=orig; 
      } completion:nil]; 

      UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:button]; 


      return backButton; 
     } 
  • LẠI NÚT HÀNH ĐỘNG

    (void) eventBack { [self.navigationController popViewControllerAnimated: YES]; }

  • UiNavigationBack Hình ảnh (Vui lòng thay đổi màu sắc của hình ảnh như yêu cầu với cùng một kích thước [25X41 144pixels/inch] để có được cái nhìn mặc định) enter image description here

0

gì làm việc cho tôi là thế này:

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName]; 
1

Cách nhanh:

Thay đổi tất cả các mục trong nav.

Trong appdelegate làm điều gì đó như thế này:

let navControl = UINavigationBar.appearance() 
    navControl.tintColor = UIColor.grayColor() 
2

Nếu bạn muốn làm điều đó trong Interface Builder mà không cần viết mã:

  1. Chọn UINavigationBar của bạn (bên UINavigationController)

enter image description here

  1. Trong thanh tra thuộc tính, tìm "Tint" - đây là những gì thiết lập màu nút Back. Lưu ý rằng "Bar Tint" không giống nhau - điều này đặt màu nền của Thanh Điều hướng.

enter image description here

Điều quan trọng để thiết lập Nút Quay lại xuất hiện theo cách này, và không cố gắng thực hiện leftBarButtonItem là, bởi vì thực hiện leftBarButtonItem sẽ vô hiệu hóa được xây dựng trong những cử chỉ chuyển hướng trở lại, như swipe để quay trở lại.

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