如何设置导航栏按钮上的图像?

时间:2011-06-22 05:04:04

标签: iphone xcode cocoa-touch uibarbuttonitem

我需要在导航栏的添加,取消和返回按钮上添加图像。我已经完成了添加按钮但没有获取后退按钮。

即使使用此添加按钮图像,它也会显示蓝色自定义按钮。为此设置?

UIBarButtonItem *addButton=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_add_button.png"] 
    style:UIBarButtonSystemItemAdd target:self               
    action:@selector(addNote)];  

1 个答案:

答案 0 :(得分:3)

// Initialize the UIButton
UIImage *buttonImage = [UIImage imageNamed:@"buttonImage.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);

// Initialize the UIBarButtonItem
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

// Set the Target and Action for aButton
[aButton addTarget:self action:@selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

// Then you can add the aBarButtonItem to the UINavigationBar
...
self.navigationItem.leftBarButtonItem = aBarButtonItem;

// Release buttonImage
[buttonImage release];