如何在导航栏按钮项中添加自定义图像?

时间:2011-05-09 07:23:29

标签: objective-c xcode ios4 iphone

 UIBarButtonItem *doneitem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)]autorelease];
    self.navigationItem.rightBarButtonItem=doneitem;

这是我的应用程序的代码,我需要在此按钮上添加图像吗?

请帮帮我。

12 个答案:

答案 0 :(得分:80)

试试这段代码:

UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(sendmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];

答案 1 :(得分:53)

 UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"XXXXXXX.png"] 
                                                  style:UIBarButtonItemStylePlain 
                                                  target:self 
                                                  action:@selector(yourMethod)];

self.navigationItem.rightBarButtonItem=_btn;

答案 2 :(得分:6)

请尝试以下代码:

UIButton *btnNext1 =[[UIButton alloc] init];
[btnNext1 setBackgroundImage:[UIImage imageNamed:@"btnNext.png"] forState:UIControlStateNormal];

btnNext1.frame = CGRectMake(100, 100, 50, 30);
UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];
[btnNext1 addTarget:self action:@selector(nextButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = btnNext;

答案 3 :(得分:5)

UIButton *urButton = [UIButton buttonWithType:UIButtonTypeCustom];
urButton.frame = urRequiredFrame;
[urButton setImage:urImage forState:UIControlStateNormal];
[urButton addTarget:self action:@selector(donePressed:)
     forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithCustomView:urButton];
self.navigationItem.rightBarButtonItem=doneButton;

答案 4 :(得分:5)

如果有人需要Swift代码来接受答案:

let infoImage = UIImage(named: "my-icon-32.png")
let imgWidth = infoImage?.size.width
let imgHeight = infoImage?.size.height
let button:UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: imgWidth!, height: imgHeight!))
button.setBackgroundImage(infoImage, forState: .Normal)
button.addTarget(self, action: Selector("openInfo"), forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)

P.S。 Gurpreet Singh的答案仅适用于透明PNG,我还必须设置除clearColor之外的按钮tintcolor。

答案 5 :(得分:3)

Swift

使用以下withRenderingMode(UIImage.RenderingMode.alwaysOriginal)方法的其他解决方案:

let img = UIImage(named: "picture")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItem.Style.plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = leftBarButtonItem

答案 6 :(得分:3)

这对我有用。我发现30x30对于导航栏中的按钮来说是个不错的尺寸。 UIImage会自动缩放到按钮大小。

UIImage *image = [UIImage imageNamed:@"XXXXXXXXXX"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted = NO;
UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightButton;

答案 7 :(得分:0)

[doneItem setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];

答案 8 :(得分:0)

一个简单的方法:

UIImage* customImg = [UIImage imageNamed:@"custom-img.png"];

UIBarButtonItem *_customButton = [[UIBarButtonItem alloc] initWithImage:customImg style:UIBarButtonItemStyleDone target:nil action:nil];

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

然后,如果您需要多个按钮,您可以只需UIBarButtonIteminitWithObjects

答案 9 :(得分:0)

这样做。这样更简单。

  1. 将图像文件放在项目目录中

  2. 将文件添加到Xcode中(右键单击Xco​​de项目,将文件添加到“项目名称”)

  3. 在故事板中选择您的UIBarButtonItem

  4. 点击“图片”找到你的图片(见截图)

  5. 庆祝,因为它会完美运作,不需要任何不必要的代码。

  6. enter image description here

答案 10 :(得分:0)

选择answer by Gurpreet Singh。要保持现有按钮的当前状态,请重新使用目标和操作。

SEL selector = self.navigationItem.rightBarButtonItem.action;
id target = self.navigationItem.rightBarButtonItem.target;
UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_close.png"]
                                                      style:UIBarButtonItemStylePlain
                                                     target:target
                                                     action:selector];

self.navigationItem.rightBarButtonItem = _btn;

答案 11 :(得分:0)

   let sliderImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 30))
    sliderImageView.contentMode = .ScaleAspectFit
    sliderImageView.image = UIImage(named: "sliderMenu")

    OR

    var sliderImage = UIImage(named: "sliderMenu")
    navigationItem.leftBarButtonItem?.image = sliderImage