我正在尝试为我的UIBarButtonItem设置图像,但我无法做到这一点.我尝试了两次,在第一种情况下,我将我的图像放在正确的位置,但是当我点击按钮时没有任何反应(它应该弹出一个新窗口,但没有任何作用).我使用的是一段代码:
UIImage *faceImage = [UIImage imageNamed:@"plus_button.png"]; UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom]; face.bounds = CGRectMake( 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;
请帮我解决问题,任何帮助都将不胜感激,谢谢!
解决方法
试试这个…改变它的方法,根据你的形象
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;