2012-09-06 33 views
21

tôi thêm một nút thanh để thanh điều hướng programitically như sauLàm thế nào để thay đổi màu font color/Tiêu đề của UIBarButtonItem trên thanh điều hướng

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" style:UIBarButtonItemStyleBordered target:self action:@selector(goToPreviousView)]; 
    self.navigationItem.leftBarButtonItem = cancel; 

Bây giờ tôi muốn hiển thị Text "CANCEL" trong RED Màu.

Tôi muốn thay đổi văn bản trên các mục nút thanh, nhưng không phải màu của nút.

Làm cách nào để thực hiện điều đó?

+0

Kiểm tra những liên kết này http://stackoverflow.com/questions/3314035/how-can-i-change -the-font-color-of-a-uibarbutton-item và http://stackoverflow.com/questions/664930/uibarbuttonitem-with-color – IronManGill

+0

Đối với iOS 5+, hãy xem câu trả lời này: http: // stackoverflow.com/questions/7810563/how-do-you-use-settitletextattributesforstate-in-uibaritem-in-ios-5-0 –

Trả lời

7

phương pháp khác là: -

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal]; 
[button setTitle:@"Delete" forState:UIControlStateNormal]; 
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f]; 
[button.layer setCornerRadius:4.0f]; 
[button.layer setMasksToBounds:YES]; 
[button.layer setBorderWidth:1.0f]; 
[button.layer setBorderColor: [[UIColor grayColor] CGColor]]; 
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0); 
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
97

Kiểm tra này ra: -

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:nil action:nil]; 
[cancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; 
+0

nó gây ra sự cố: Chủ đề 1: EXC_BAD_ACESS (mã = 2 địa chỉ = 0x0) – user1645721

+1

+1 nó hoạt động hoàn hảo.thanks @ user1645721 setTitleTextAttributes chức năng có sẵn từ IOS 5 –

+1

Đây là cách dễ dàng hơn để thực hiện hơn các giải pháp được chấp nhận và nó hoạt động ... – c0d3Junk13

4

mã này được sử dụng để thay đổi màu sắc văn bản của UIBarButtonItem trên thanh điều hướng:

UILabel *lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 25)]; 
lblTotCaratteri.textAlignment = UITextAlignmentCenter; 
lblTotCaratteri.font = [UIFont italicSystemFontOfSize:13.0]; 
lblTotCaratteri.textColor = [UIColor redColor]; 
lblTotCaratteri.backgroundColor = [UIColor clearColor]; 
lblTotCaratteri.adjustsFontSizeToFitWidth = YES; 
lblTotCaratteri.text = @"Cancel"; 

UIBarButtonItem *lblCaratteri = [[UIBarButtonItem alloc] initWithCustomView: lblTotCaratteri]; 

self.navigationItem.rightBarButtonItem = lblCaratteri; 
10
UITextAttributeTextColor //Is deprecated on iOS 7. 

Mã này là chúng tôi ed để thay đổi màu văn bản từ proxy xuất hiện.

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

Điều này đặt màu nền, ông yêu cầu màu văn bản. – Lukasz

+0

Sử dụng NSForegroundColorAttributeName thay vì UITextAttributeTextColor – eXhausted

0

UITextAttributeTextColor // là bị phản đối trên iOS 7.

Đặt màu của BarButtonItem theo một cách như thế này

[_barButtonItem setTitleTextAttributes: 
        [NSDictionary dictionaryWithObjectsAndKeys: 
          [UIColor colorWithRed:250/255.0 
              green:240/255.0 
              blue:230/255.0 
              alpha:1.0], 
          NSForegroundColorAttributeName,nil] 
        forState:UIControlStateNormal]; 
27

Chỉ cần một iOS7 Cập nhật với Modern obj-C Cú pháp:

[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal]; 
2

Câu hỏi cũ, đây là giải pháp nhanh chóng 2.2:

let cancel = UIBarButtonItem(title: "CANCEL", style: .Bordered, target: self, action: #selector(goToPreviousView)) 
    cancel.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Normal) 
    self.navigationItem.leftBarButtonItem = cancel 
0

Chính điều đó tất cả mọi người nên làm gì nếu nó không phải là dự án của bạn và bạn chỉ cần thêm một số thay đổi - là kiểm tra

[UIBarButtonItem appearance] 

tôi đã lãng phí rất nhiều thời gian để nhận ra rằng một người nào đó đặt giao diện sai UIBarButtonItem

0

đây được cập nhật nhanh chóng mã phiên bản 4.0:

let reset = UIBarButtonItem(title: "Reset All", style: .plain , target: self, action: #selector(self.resetButtonClicked(_ :))) 
      reset.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal) 
Các vấn đề liên quan