質問

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.

役に立ちましたか?

解決

Have you tried this? :-)

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

Best, Sascha

他のヒント

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.

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top