Create a button programmatically and set a background image

When I try to create a button and set the background image to Swift:

let button = UIButton.buttonWithType(UIButtonType.System) as UIButton button.frame = CGRectMake(100, 100, 100, 100) button.setImage(IMAGE, forState: UIControlState.Normal) button.addTarget(self, action: "btnTouched:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button) 

I always get the error message: " Unable to convert the expression type" Void "to type" UIImage " ".

+69
ios swift uibutton
Jun 05 '14 at 15:52
source share
11 answers

Your code should look below

 let image = UIImage(named: "name") as UIImage? let button = UIButton(type: UIButtonType.Custom) as UIButton button.frame = CGRectMake(100, 100, 100, 100) button.setImage(image, forState: .Normal) button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside) self.view.addSubview(button) 
+128
Jun 05 '14 at
source share

Try the following:

 let image = UIImage(named: "ImageName.png") as UIImage var button = UIButton.buttonWithType(UIButtonType.System) as UIButton button.frame = CGRectMake(100, 100, 100, 100) button .setBackgroundImage(image, forState: UIControlState.Normal) button.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside) menuView.addSubview(button) 

Let me know if this works or not?

+24
Jul 01
source share

SWIFT version 3 of Alex Reynolds answer

 let image = UIImage(named: "name") as UIImage? let button = UIButton(type: UIButtonType.custom) as UIButton button.frame = CGRect(x: 100, y: 100, width: 100, height: 100) button.setImage(image, for: .normal) button.addTarget(self, action: Selector("btnTouched:"), for:.touchUpInside) self.view.addSubview(button) 
+8
Feb 27 '17 at 16:21
source share

Create a button and add an image as a Swift 4 background

  let img = UIImage(named: "imgname") let myButton = UIButton(type: UIButtonType.custom) myButton.frame = CGRect.init(x: 10, y: 10, width: 100, height: 45) myButton.setImage(img, for: .normal) myButton.addTarget(self, action: #selector(self.buttonClicked(_:)), for: UIControlEvents.touchUpInside) self.view.addSubview(myButton) 
+6
Oct 05 '15 at 10:23
source share

You need to check that the image is not zero before assigning it to the button.

Example:

 let button = UIButton.buttonWithType(UIButtonType.System) as UIButton button.frame = CGRectMake(100, 100, 100, 100) if let image = UIImage(named: "imagename.png") { button.setImage(image, forState: .Normal) } button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside) self.view.addSubview(button) 
+5
Nov 13 '14 at 5:53
source share
 let btnRight=UIButton.buttonWithType(UIButtonType.Custom) as UIButton btnRight.frame=CGRectMake(0, 0, 35, 35) btnRight.setBackgroundImage(UIImage(named: "menu.png"), forState: UIControlState.Normal) btnRight.setTitle("Right", forState: UIControlState.Normal) btnRight.tintColor=UIColor.blackColor() 
+4
Mar 18 '15 at 6:57
source share

Here's how you can create a pretty button with a border and rounded edges:

 loginButton = UIButton(frame: CGRectMake(self.view.bounds.origin.x + (self.view.bounds.width * 0.325), self.view.bounds.origin.y + (self.view.bounds.height * 0.8), self.view.bounds.origin.x + (self.view.bounds.width * 0.35), self.view.bounds.origin.y + (self.view.bounds.height * 0.05))) loginButton.layer.cornerRadius = 18.0 loginButton.layer.borderWidth = 2.0 loginButton.backgroundColor = UIColor.whiteColor() loginButton.layer.borderColor = UIColor.whiteColor().CGColor loginButton.setTitle("Login", forState: UIControlState.Normal) loginButton.setTitleColor(UIColor(red: 24.0/100, green: 116.0/255, blue: 205.0/205, alpha: 1.0), forState: UIControlState.Normal) 
+4
Apr 25 '15 at 20:04
source share

To set the background, we can no longer use forState , so we must use for to set UIControlState :

 let zoomInImage = UIImage(named: "Icon - plus") as UIImage? let zoomInButton = UIButton(frame: CGRect(x: 10), y: 10, width: 45, height: 45)) zoomInButton.setBackgroundImage(zoomInImage, for: UIControlState.normal) zoomInButton.addTarget(self, action: #selector(self.mapZoomInAction), for: .touchUpInside) self.view.addSubview(zoomInButton) 
+2
Nov 30 '17 at 7:11
source share

Upgrade to Swift 3

 let image = UIImage(named: "name") let button = UIButton(type: .custom) button.frame = CGRect(x: 100, y: 100, width: 100, height: 100) button.setImage(image, for: .normal) button.addTarget(self, action: #selector(self.btnAbsRetClicked(_:)), for:.touchUpInside) self.view.addSubview(button) 
+2
Dec 12 '17 at 12:57
source share

Swift: Ui button creates programmatically

 let myButton = UIButton() myButton.titleLabel!.frame = CGRectMake(15, 54, 300, 500) myButton.titleLabel!.text = "Button Label" myButton.titleLabel!.textColor = UIColor.redColor() myButton.titleLabel!.textAlignment = .Center myButton.addTarget(self,action:"Action:",forControlEvents:UIControlEvent.TouchUpInside) self.view.addSubview(myButton) 
+1
Apr 23 '15 at 7:44
source share

Swift 5 version of the accepted answer:

 let image = UIImage(named: "image_name") as UIImage? let button = UIButton(type: UIButton.ButtonType.custom) as UIButton button.frame = CGRect(x: 100, y: 100, width: 200, height: 100) button.setImage(image, for: .normal) button.addTarget(self, action: #selector(function), for: .touchUpInside) //button.backgroundColor = .lightGray self.view.addSubview(button) 

where of course

 @objc func function() {...} 

The image is centered by default. You can change this by setting the imageEdgeInsets button, for example:

 // In this case image is 40 wide and aligned to the left button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: button.frame.width - 45) 
0
Jun 21 '19 at 10:00
source share



All Articles