1

I want to change text color of UIBarButtonItem,like Pinterest app(navigationItem.backBarButtonItem). But here's almost same question. Is there an easy way to change the text color of a UIBarButtonItem without using an image?

So,Why text color of UIBarButtonItem is changed in Pinterest app?

Community
  • 1
  • 1
Yu Tamura
  • 143
  • 1
  • 9

4 Answers4

4

Try setTitleTextAttributes:forState:

You can learn more from here

MasterBeta
  • 606
  • 6
  • 15
0
 @interface MyViewCOontroller : UIViewController {
  UILabel *lblTotCaratteri;
  }

In implementation

  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];

hope this helps

0

I don't the direct way of doing this but you can achieve this by adding a UIBUtton to the customview of the UIBarButtonItem.

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;

And this will give you a UIBarButtonItem with the text "Back" in red color.

superGokuN
  • 1,399
  • 13
  • 28
0

You can use UISegmentedControl to simulate the UIBarButtonItem. Make sure you have a segment control with single segment.

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

Retrieve the label of the UISegmentedControl and set the textColor to your preferred color

UILabel* lblText = [[[[YOUR_SEGMENTED_CONTROL subviews] objectAtIndex:0] subviews] objectAtIndex:0];
lblText.textColor = [UIColor blackColor];
grassyburrito
  • 1,213
  • 22
  • 32