ChatGPT解决这个技术问题 Extra ChatGPT

在代码中为 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];

但看不到什么会为它设置图像。


M
Michal Šrůtek

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)

作为旁注:这将显示图像,但按钮标题文本将被隐藏。
这是代码完全可以工作,但我正在通过解析获取图像,所以我怎么能做到这一点,因为在 UIImage 案例中这段代码对我有用:- largePick.image = aNewsInfo.smallImageData;我怎么能用 UIButton 做到这一点...你能帮帮我吗...
@slcott,the documentation 在哪里说明?我没有看到它表明 setImage 已被弃用的任何地方。 (在我看来,您将 UIButton.setImageUITableViewCell.image 混淆了,如您所说,UITableViewCell.image 一个已弃用的属性。)
注意:如果你使用 UIButtonTypeCustom 以外的任何东西,图像在 iOS7 和 8 中将是蓝色的。
对于 Swift 3:btnTwo.setImage(btnImage, for: UIControlState.normal)
l
leviathan

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

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

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

setImage 和 setBackgroundImage 有什么区别?
setBackgroundImage 将在标题文本上方的按钮宽度上拉伸图像,而 setImage 将使按钮成为忽略标题文本而不拉伸图像的图像。
A
Alex Cio

在这对我有用之前,我必须根据图像帧大小显式调整按钮框架的大小。

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;

@jrasmusson 只是想补充一点,摆弄框架的 3 行也可以写成 [listButton sizeToFit]
S
Sruit A.Suk

如果是 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) 

A
Azhar

你可以这样做

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

A
Alex Cio
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];

A
Alex Cio

您可以以任何一种方式放置图像:

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];

这部分代码让我感到困惑。 btnImage = [UIImage imageNamed:@"image.png"]; 什么是btnImage?那不在原始代码中。这是一个新按钮吗?
您可以直接设置按钮的图像,而不是像这样获取另一个图像对象: [btnTwo setImage:[UIImage imageNamed:@"image.png"]];
您必须将 buttonWithType:UIButtonTypeRoundedRect 设置为 UIButtonTypeCustom 其他明智的按钮将不会按照您的图像显示。
A
Alex Cio

我正在寻找一种将 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 时,我只需将 var hidden 设置为 YESNO。可能还有其他解决方案,但我对这些东西困惑了很多次,这解决了它,我不需要处理 UIButton 在后台做的内部事情。


P
Peter Schultz

不用担心从代码中构建按钮,您可以在情节提要上做到这一点。这对我有用,一条线......更简单。

[self.button setBackgroundImage:[UIImage imageNamed: @"yourPic.png"] forState:UIControlStateNormal];

P
P.J.Radadiya
-(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....
    }
}

F
Frank Eno

Swift 3 版本(butt_img 必须是 Xcode 中 Assets.xcassets 或 Images.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)

A
ArNo

对于迅捷 5+

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