Search code examples
iosswiftxcodenslayoutconstraint

Why setting "translatesAutoresizingMaskIntoConstraints" to false before setting imageView, will show no images?


I am really new about iOS, I am learning about constraints in a scroll view.

I have the following code (type1):

wallpaper.view.translateAutoResizingMaskIntoConstraints = false
wallpaper.wallpaperImage = UIImage(named: backgroundImageName)

If I build the app, the screen only shows white screen. I have checked nil possibility of my image but it's not.

Then I swapped the code into this (type2):

wallpaper.wallpaperImage = UIImage(named: backgroundImageName)
wallpaper.view.translateAutoResizingMaskIntoConstraints = false

Then, the image would show up. My question is why does it affect my imageView? I have seen several codes using type1 and they work normally.

Thank you for reading!


Solution

  • I think is because the intrinsic content size of your the imageView while image = nil is CGSize.zero only if you define width and height constraint explicitly you will get a different result, then when you set the image before that the intrinsic content size of your UIImageView will have a value defined by the UIImage contained in the UIImageView