Search code examples
iosswiftuiimageviewuiimage

swift UIImage blue square


I have a project where any image I try to call using .setImage shows a blue square instead of the image.

sender.setImage(UIImage(named: "o.png"), for: [])
print(sender.tag)

any help is greatly appreciated

Edit: I forgot to mention this is setting an image for a button inside of an IBAction


Solution

  • A couple things. I've never seen setImage used with an empty array of states. Even though the docs say "if the state isn't specified, Normal will be used", try explicitly stating the states where you want your image to be applied:

    Try doing this:

    // let's make sure sender is really a button
    if let aButton = sender as? UIButton
    {
        if let nonBlueImage = UIImage(named: "o.png")
        {
            aButton.setImage(nonBlueImage, for: [.normal, .selected, .highlighted])
        } else {
            print("couldn't find o.png")
        }
    } else {
        print("suprise, sender wasn't really a button")
    }