无法为UIBarButtonItem设置图像

时间:2012-06-30 12:25:59

标签: ios objective-c iphone uitableview uibarbuttonitem

我正在尝试为我的UIBarButtonItem设置图像,但我无法做到这一点。我尝试了两次,在第一种情况下,我将我的图像放在正确的位置,但是当我点击按钮时没有任何反应(它应该弹出一个新窗口,但没有任何效果)。我使用了一段代码:

UIImage *faceImage = [UIImage imageNamed:@"plus_button.png"];
    UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
    face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
    [face setImage:faceImage forState:UIControlStateNormal];
   UIBarButtonItem *faceBtn = [[[UIBarButtonItem alloc] initWithCustomView:face]initWithImage:faceImage style:UIBarButtonItemStylePlain target:self action:@selector(addProduct:)];

    self.navigationItem.leftBarButtonItem = faceBtn;

在第二种情况下,我在按钮上设置图像,并且应该出现新窗口,但是不仅我想要的自定义图像,而且它还显示“边框”,看起来图像被放在默认的中心按钮。显然我只想要我的图像,而不仅仅是我的图像。我在第二种情况下使用了一段代码:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style:UIBarStyleDefault target:self action:@selector(addProduct:)];

    self.navigationItem.leftBarButtonItem = addButton;

请帮我解决问题,任何帮助将不胜感激,谢谢!

5 个答案:

答案 0 :(得分:9)

试试这个...改变它的方法,根据你的图像

UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[button1 addTarget:self action:@selector(showLeft:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:@"left-arrow-button.png"] forState:UIControlStateNormal];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.navigationItem.leftBarButtonItem = button;

答案 1 :(得分:3)

我修改了您的代码并测试了按钮中显示的图像。请自己尝试一下。如果你遇到任何问题,请告诉我。

    UIImage *faceImage = [UIImage imageNamed:@"Badge.png"];
    UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
    face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
    [face addTarget:self action:@selector(showAlerts) forControlEvents:UIControlEventTouchUpInside];
    [face setImage:faceImage forState:UIControlStateNormal];
    UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];    
    self.navigationItem.leftBarButtonItem = faceBtn;
编辑:我在答案中添加了一句话。我刚刚在日志中打印了一条消息“嗨”。这是工作。你能试试吗?

感谢。

答案 2 :(得分:3)

或者......只需编写两行iOS 8 Swift代码。 (甚至可能在iOS 7中工作)

    let doneButtonAsLeftArrow = UIBarButtonItem(image: UIImage(named: "LeftArrow24x24.png"), style: .Plain, target: self, action: "doneButtonPushed")
    navigationItem.leftBarButtonItem = doneButtonAsLeftArrow

答案 3 :(得分:1)

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style: UIButtonTypeCustom target:self action:@selector(addProduct:)];

    self.navigationItem.leftBarButtonItem = addButton;

您正在创建默认按钮,您需要创建一个UIButtonTypeCustom类型按钮...

答案 4 :(得分:1)

任何人接受的答案的快速版本

var settingsButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
settingsButton.frame = CGRect(origin: CGPointZero, size: CGSize(width: 36, height: 36))
settingsButton.setImage(image, forState: UIControlState.Normal)
settingsButton.addTarget(self, action: "buttonActionHere:", forControlEvents: UIControlEvents.TouchUpInside)

let rightBarButtonItem = UIBarButtonItem(customView: settingsButton)
navigationItem.setRightBarButtonItem(rightBarButtonItem, animated: true)