Search code examples
iosswiftuiimageviewchildviewcontrollerparentviewcontroller

How to change UIImageView as background image on parentViewController using swift


I would like to change my UIImageView I used as a background image on my parentViewController after my childView is loaded

in my viewDidLoad, I have this line, that pretty works :

self.parentViewController?.view.addSubview(myBackgroundImage)

I wish to change it while I got an event on app. But when I try again to change it by using the same line as above it doesn't work.

Anyone have a solution ?


Solution

  • I think it is a bad practice to access the view from another controller. I will suggest you to create separate method for this. Or may be just use IBoutlet for this, for example:

    self.parentViewController?.myBackgroundImageView.image = UIImage("your_image")