1

I'm trying to learn how to use autolayout programmatically in Swift using this tutorial. I added the PNG to the Assets folder, but when I try to add an UIImageView in the ViewController let imageView = UIImageView(image: bear) I get the error "use of unresolved identifier 'bear'".

However, if I go to the Main.storyboard, I can add the image fine. Any tips on why this image isn't recognized in the ViewController? (I tried this with multiple PNGs in case it was a bad image, but same result).

Tiffany Xu
  • 33
  • 3
  • 1
    https://stackoverflow.com/a/52386885/603127 – Okan Kocyigit May 05 '19 at 14:36
  • I get the exact same error when I use the syntax you have in your question - try putting double-quotes around `bear`... it thinks that's some kind of variable. –  May 05 '19 at 15:37

1 Answers1

1

bear needs to be a type UIImage, try something like this

func addImage() {
     let imageView = UIImageView()
     let bear = UIImage(named: "bear") // whatever the name of that image file is, within your assets

     imageView.image = bear // setting your bear image here
}

jaelee
  • 46
  • 4
  • Thanks! This works, but I am just curious why `imageView = UIImageView(image: bear)` doesn't work since this works in the video tutorial? – Tiffany Xu May 05 '19 at 15:50
  • The `UIImageView(image: UIImage) ` . That function is expecting a `UIImage` as its parameter – jaelee May 05 '19 at 18:51