将导航栏标题设置为UIBarButtonItem?

时间:2013-12-29 07:44:52

标签: objective-c ios7 xcode5

这里是我用来将UIBarButtonItems添加到导航栏的一些代码,如何更改显示自定义图像或上传到我的Xcode项目的图像?

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;

2 个答案:

答案 0 :(得分:2)

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

此外,您不仅可以使用此方法设置图像,还可以设置自定义UIView:

- (id)initWithCustomView:(UIView *)customView

See more info.

答案 1 :(得分:0)

在iOS 8中不推荐使用

UIBarButtonItemStyleBordered。请改用UIBarButtonItemStylePlain

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