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

但是看不到会为其设置图像。

Objective-C的

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

迅速

 let btnImage = UIImage(named: "image") btnTwo.setImage(btnImage , forState: UIControlState.Normal) 

迈克的解决scheme将只显示图像,但button上设置的任何标题将不可见,因为您可以设置标题或图像。

如果你想设置(图像和标题)使用下面的代码:

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

在此之前,我不得不根据图像帧大小显式调整button框架。

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

在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) 

你可以把图像放在任何一个方面:

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

我正在寻找一个解决scheme来添加一个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的var设置为YESNO 。 有可能是其他的解决scheme,但我很多这个东西困惑,这解决了它,我不需要处理内部的东西UIButton在后台做。

不要太担心从代码框架button,你可以在故事板上做到这一点。 这对我来说,一条线…更简单。

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

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

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

无论如何,如果你想缩放图像来填充button的大小,你可以添加一个UIImageView并为其指定图像:

 let img = UIImageView() img.frame = btnTwo.frame img.contentMode = .scaleAspectFill img.clipsToBounds = true img.image = UIImage(named: "butt_img") btnTwo.addSubview(img) 
 -(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.... } }