有没有办法改变UIBarButtonItem的文字颜色?

时间:2012-04-11 05:11:14

标签: iphone objective-c uibarbuttonitem uinavigationitem

我想更改UIBarButtonItem的文本颜色,如Pinterest app(navigationItem.backBarButtonItem)。 但这是几乎相同的问题。 Is there an easy way to change the text color of a UIBarButtonItem without using an image?

那么,为什么在Pinterest应用程序中更改了UIBarButtonItem的文本颜色?

4 个答案:

答案 0 :(得分:4)

尝试setTitleTextAttributes:forState:

您可以从here

了解更多信息

答案 1 :(得分:0)

 @interface MyViewCOontroller : UIViewController {
  UILabel *lblTotCaratteri;
  }

在实施中

  UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
 lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 15)];
 lblTotCaratteri.textAlignment = UITextAlignmentCenter;

 lblTotCaratteri.textColor =[UIColor greyColor];

希望这会有所帮助

答案 2 :(得分:0)

我不直接这样做,但你可以通过在UIBarButtonItem的customview中添加一个UIBUtton来实现这一点。

UIButton *backButton    =   [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame        =   CGRectMake(0, 0, 80, 40);
[backButton setBackgroundImage:[UIImage imageNamed:@"back-button.png"] forState:UIControlStateNormal];// Setting the background Image
[backButton setTitle:@"Back" forState:UIControlStateNormal];//Setting the title of the button
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];// Setting the text color.
[backButton addTarget:self action:@selector(backButtonTaped) forControlEvents:UIControlEventTouchUpInside];

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

self.navigationItem.leftBarButtonItem   =   backButtonItem;

这将为您提供一个UIBarButtonItem,文本“Back”为红色。

答案 3 :(得分:0)

您可以使用UISegmentedControl来模拟UIBarButtonItem。确保您有一个单段的段控件。

- (void) viewDidLoad {
  [YOUR_SEGMENTED_CONTROL removeAllSegments];
  [YOUR_SEGMENTED_CONTROL insertSegmentWithTitle:@"MyButton" atIndex:0 animated:NO];   
}

检索UISegmentedControl的标签并将textColor设置为您喜欢的颜色

UILabel* lblText = [[[[YOUR_SEGMENTED_CONTROL subviews] objectAtIndex:0] subviews] objectAtIndex:0];
lblText.textColor = [UIColor blackColor];