9

我正在尝试为我的 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;

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

4

5 回答 5

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;
于 2012-06-30T13:12:46.260 回答
4

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

    let doneButtonAsLeftArrow = UIBarButtonItem(image: UIImage(named: "LeftArrow24x24.png"), style: .Plain, target: self, action: "doneButtonPushed")
    navigationItem.leftBarButtonItem = doneButtonAsLeftArrow
于 2015-02-15T15:20:39.243 回答
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;

编辑:我在答案中添加了一行。我刚刚在日志中打印了一条消息“嗨”。这是工作。你能试试吗?

谢谢。

于 2012-06-30T12:32:02.093 回答
1

任何人都接受的答案的 Swift 版本

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)
于 2014-11-23T04:20:37.633 回答
0
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:faceImage style: UIButtonTypeCustom target:self action:@selector(addProduct:)];

    self.navigationItem.leftBarButtonItem = addButton;

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

于 2012-06-30T12:33:06.513 回答