Ios 在代码中为UIButton设置图像
如何在代码中设置UIButton的图像 我有这个:Ios 在代码中为UIButton设置图像,ios,objective-c,iphone,ipad,uibutton,Ios,Objective C,Iphone,Ipad,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)
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];
但是看不到什么会为它设置图像。Objective-C
UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
Swift 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];
我正在寻找一种解决方案,将
ui图像
添加到我的ui按钮
。问题是它显示的图像比需要的大。只是帮了我这个忙:
_imageViewBackground = [[UIImageView alloc] initWithFrame:rectImageView];
_imageViewBackground.image = [UIImage imageNamed:@"gradientBackgroundPlain"];
[self addSubview:_imageViewBackground];
[self insertSubview:_imageViewBackground belowSubview:self.label];
_imageViewBackground.hidden = YES;
每次我想显示我的UIImageView
时,我只是将varhidden
设置为YES
或NO
。
可能还有其他的解决方案,但我多次被这些东西弄糊涂了,这就解决了问题,我不需要处理内部的东西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];
Swift 3版本(butt\u img必须是设置在Xcode中Assets.xcsets或Images.xcsets文件夹中的图像): 无论如何,如果要缩放图像以填充按钮大小,可以在其上添加
UIImageView
,并将其指定为您的图像:
let img = UIImageView()
img.frame = btnTwo.frame
img.contentMode = .scaleAspectFill
img.clipsToBounds = true
img.image = UIImage(named: "butt_img")
btnTwo.addSubview(img)
作为旁注:这将显示图像,但按钮标题文本将被隐藏。这是代码,但我正在通过解析获取图像,因此如何才能做到这一点,因为这段代码在UIImage案例中适用:-largePick.image=aNewsInfo.smallImageData;我怎样才能用UIButton做到这一点。。。你能帮我吗…@slcott,你在哪里说的?我看不出任何地方有人建议不推荐使用
setImage
。(在我看来,您将UIButton.setImage
与UITableViewCell.image
混淆,正如您所述,这是一个不推荐使用的属性。)注意:如果您使用uibuttonySpecCustom以外的任何内容,则在iOS7和IOS8中图像将为蓝色。对于Swift 3:btnTwo.setImage(btnImage,对于:UIControlState.normal)这部分代码让我感到困惑<代码>btnImage=[UIImage ImageName:@“image.png”]代码>什么是btnImage?原始代码中没有。这是一个新按钮吗?您可以直接设置按钮的图像,而不是像这样拍摄另一个图像对象:[btnTwo setImage:[UIImage imagename:@“image.png”];您必须拥有以下类型的按钮:UIButtonyPeroundRect to UIButtonyPecustom other wise按钮将不会按照您的图像显示。setImage和setBackgroundImage之间的区别是什么?setBackgroundImage将在标题文本上的按钮宽度上拉伸图像,而setImage将使按钮成为忽略标题的图像文本,而不拉伸图像。@jrasmusson只是想补充一点,即3行对框架的摆弄也可以用[listButton sizeToFit]
-(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....
}
}
btnTwo.setBackgroundImage(UIImage(named: "butt_img"), for: .normal)
btnTwo.setTitle("My title", for: .normal)
let img = UIImageView()
img.frame = btnTwo.frame
img.contentMode = .scaleAspectFill
img.clipsToBounds = true
img.image = UIImage(named: "butt_img")
btnTwo.addSubview(img)