Chisx
Chisx

Reputation: 1986

Set navigation bar title to UIBarButtonItem?

here's some code that I am using to add UIBarButtonItems to my navigation bar, how can I change it display custom images, or images uploaded to my Xcode project?

UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];

NSArray *actionButtonItems = @[shareItem, cameraItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;

Upvotes: 0

Views: 219

Answers (2)

Ilya Dmitriev
Ilya Dmitriev

Reputation: 1730

UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];

self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: shareItem, cameraItem, nil];

Also you can set not only your image, but also your custom UIView with this method:

- (id)initWithCustomView:(UIView *)customView

See more info.

Upvotes: 2

Nevin Chen
Nevin Chen

Reputation: 1815

UIBarButtonItemStyleBordered is deprecated in iOS 8. Use UIBarButtonItemStylePlain instead.

UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_clear"] style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *actionButtonItems = @[shareItem];
self.navigationItem.leftBarButtonItems = actionButtonItems;

Upvotes: 0

Related Questions