简体   繁体   中英

Swift Button Background Image not appearing?

I have some very simple code to just programmatically set a button's background image to something but when the simulator runs it shows nothing. On the storyboard it shows that the image is present however, and again nothing shows.

class ViewController: UIViewController {

@IBOutlet weak var LetUsOutlet: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    let backgroundColor = UIColor(red: 0, green: 255, blue: 247, alpha: 1)
    self.view.backgroundColor = backgroundColor
    let backgroundButtonImage = UIImage(named: "round rectangle button.png") as UIImage?
    self.LetUsOutlet.setImage(backgroundButtonImage, for: .normal)
}

@IBAction func LetUsCreateMeal(_ sender: UIButton) {
}
@IBAction func CreateYourOwn(_ sender: UIButton) {
}
@IBAction func BrowseItems(_ sender: UIButton) {
}
@IBAction func ViewYourMeals(_ sender: UIButton) {
}

}

This is the simple code that I have so I am not sure why it isn't working properly. Below is my storyboard.

http://imgur.com/gallery/c5EWW

The first button is where I try to programatically set the background, and the other 3 are when I just set the background image property within the storyboard.

This is what happens when it runs, showing that all background images are empty.

http://imgur.com/gallery/urM25

Any help is appreciated.

EDIT: I seemed to have the image stored in the wrong place, I put it in a separate folder and not in the xassets folder.

要更改按钮背景图片,您应该使用

LetUsOutlet.setBackgroundImage(UIImage(named:"round rectangle button.png"), forState: UIControlState.Normal)

I mocked up an example - seemed to work okay for me. I suggest you check your button's constraints in IB

小样

change this line

let backgroundButtonImage = UIImage(named: "round rectangle button.png") as UIImage?

to this line

let backgroundButtonImage = UIImage(named: "round rectangle button")

and this line

self.LetUsOutlet.setImage(backgroundButtonImage, for: .normal)

to this

self.LetUsOutlet.setBackgroundImage(backgroundButtonImage, for: .normal)

Im having the same issue with the latest Xcode 13.1:

Im just trying to simply set the background image of the button, but whatever I try it is doing nothing. I also put a UIImageView there to see if there's any issue with the image file, but the Image View is set perfectly fine. Do I maybe have to set something in the properties of the button??

Here the code:

   import UIKit

   class ViewController: UIViewController {




   override func viewDidLoad() {
    super.viewDidLoad()
    
    let imageToSet = UIImage(named: "3-1")
    
    b1.setBackgroundImage(imageToSet, for: .normal)
    
    imageView.image = imageToSet
   }



@IBOutlet weak var b1: UIButton!

@IBOutlet weak var imageView: UIImageView!

   }

These are my setting for the button:

在此处输入图片说明

Thanks for your help!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM