mvasco
mvasco

Reputation: 5107

UIBarButtonItem image and color

I need to add a UIBarButtonItem programmatically to the navigation bar of a viewController.

I am using the following code to do it, but it only shows the button text, and I want to show the system default rewind button and change its default color:

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemRewind target:self
                                                                 action:@selector(backButton:)];
self.navigationItem.rightBarButtonItem = backButton;

The button action is correct. Any help is welcome.

Upvotes: 1

Views: 547

Answers (3)

Wain
Wain

Reputation: 119021

You're trying to use an item type as a style, which you can't do, they are different enum types.

You should consider creating a custom image to use on your bar button. Or, creating a view which contains a label and an image and setting that as the custom view of the bar button.

Upvotes: 1

Have you tried this? :-)

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:selector(backButton:)];
backButton.tintColor = [UIColor redColor];

Best, Sascha

Upvotes: 1

Léo Natan
Léo Natan

Reputation: 57040

You need to use initWithBarButtonSystemItem:target:action: for system items. To change the color of the item, set the tintColor of the navigation bar to the color you need.

Upvotes: 1

Related Questions