在代码中为UIButton设置一个图像

如何在代码中设置UIButton的图像?

我有这个:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTwo];

但不知道什么会为它设置图像。

311858 次浏览

objective - c

UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];

斯威夫特5.1

let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: .normal)

Mike的解决方案将只显示图像,但在按钮上设置的任何标题都将不可见,因为您可以设置标题或图像。

如果你想同时设置(你的图片和标题),请使用以下代码:

btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];

你可以把图像放在两种方式中的任何一种:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self
action:@selector(goToOne)
forControlEvents:UIControlEventTouchUpInside];




[btnTwo setImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];


//OR setting as background image


[btnTwo setBackgroundImage:[UIImage imageNamed:@"name.png"]
forState:UIControlStateNormal];


[self.view addSubview:btnTwo];

在这之前,我必须根据图像帧大小显式地调整按钮帧的大小。

UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];


// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;


[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController
action:@selector(revealToggle:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:listButton];


self.navigationItem.leftBarButtonItem = jobsButton;

你可以这样做

[btnTwo setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 150, 44);
[btn setBackgroundImage:[UIImage imageNamed:@"buttonimage.png"]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(btnSendComment_pressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

我正在寻找一个解决方案,将UIImage添加到我的UIButton。问题是它显示的图像比需要的大。刚刚帮了我这个

_imageViewBackground = [[UIImageView alloc] initWithFrame:rectImageView];
_imageViewBackground.image = [UIImage imageNamed:@"gradientBackgroundPlain"];
[self addSubview:_imageViewBackground];
[self insertSubview:_imageViewBackground belowSubview:self.label];
_imageViewBackground.hidden = YES;
每次我想显示我的UIImageView时,我只需将变量hidden设置为YESNO。 可能有其他的解决方案,但我对这个东西感到困惑了很多次,这个解决了它,我不需要处理内部的东西UIButton正在后台做的

如果是Swift用户

// case of normal image
let image1 = UIImage(named: "your_image_file_name_without_extension")!
button1.setImage(image1, forState: UIControlState.Normal)


// in case you don't want image to change when "clicked", you can leave code below
// case of when button is clicked
let image2 = UIImage(named: "image_clicked")!
button1.setImage(image2, forState: UIControlState.Highlight)

不用太担心从代码中构造按钮,你可以在故事板中完成。这对我很管用,一句台词……更简单。

[self.button setBackgroundImage:[UIImage imageNamed: @"yourPic.png"] forState:UIControlStateNormal];
-(void)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;


if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"icon-Locked.png"]])
{
[btn setImage:[UIImage imageNamed:@"icon-Unlocked.png"] forState:UIControlStateNormal];
// other statements....
}
else
{
[btn setImage:[UIImage imageNamed:@"icon-Locked.png"] forState:UIControlStateNormal];
// other statements....
}
}

Swift 3版本 (butt_img必须是Xcode中Assets.xcassetsImages.xcassets文件夹中的图像集):

btnTwo.setBackgroundImage(UIImage(named: "butt_img"), for: .normal)
btnTwo.setTitle("My title", for: .normal)

无论如何,如果你想让图像缩放到按钮的大小,你可以在它上面添加UIImageView并将你的图像分配给它:

let img = UIImageView()
img.frame = btnTwo.frame
img.contentMode = .scaleAspectFill
img.clipsToBounds = true
img.image = UIImage(named: "butt_img")
btnTwo.addSubview(img)

swift 5+

let image = UIImage(named: "icons-multiply")?.withRenderingMode(.alwaysTemplate)
btn.setImage(image, for: .normal)