title: Refresh UIImageView's Image
body: <p>I have a question that has probably been asked a lot, but I just can't get it. I'm making an iOS tabbed app with Swift 5 and Xcode 11. I have a background UIImageView that changes the image every time a user presses a button. The image actually displays pretty well, there's just one thing: The background image doesn't change, it just takes the previous image. Here's my code:</p>
  
  <pre class="lang-swift prettyprint-override"><code>
      func theme(){
          let backgroundImage = UIImageView(frame: self.view1.bounds)
          backgroundImage.autoresizingMask = [.flexibleHeight, .flexibleWidth]
          self.view1.insertSubview(backgroundImage, at: 0)
          if UserDefaults.standard.integer(forKey: "like") == 1{
              backgroundImage.image = nil
              backgroundImage.image = UIImage(named: "img1")
              self.tabBarController?.tabBar.barTintColor = UIColor(red:0.84, green:0.84, blue:0.84, alpha:1.0)
              self.tabBarController?.tabBar.tintColor = UIColor(red:0.21, green:0.56, blue:0.96, alpha:1.0)
              ansView.backgroundColor = UIColor.white
              historyButton.backgroundColor = UIColor.black
              for button in operatorButtons {
                  button.backgroundColor = UIColor.black
              }
              for button in numberButtons {
                  button.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:0.5)
              }
              equalButton.backgroundColor = UIColor(red:1.00, green:0.49, blue:0.18, alpha:1.0)
              self.view.backgroundColor = UIColor(red:0.78, green:0.78, blue:0.78, alpha:1.0)
              view1.contentMode = UIView.ContentMode.scaleAspectFill
          }else if UserDefaults.standard.integer(forKey: "like") == 2{
              self.tabBarController?.tabBar.barTintColor = UIColor.black
              self.tabBarController?.tabBar.tintColor = UIColor(red:0.98, green:0.64, blue:0.02, alpha:1.0)
              backgroundImage.image = nil
              backgroundImage.image = UIImage(named: "img2")
              ansView.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:1)
              historyButton.backgroundColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.9)
              for button in operatorButtons {
                  button.backgroundColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.9)
              }
              for button in numberButtons {
                  button.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:0.5)
              }
              equalButton.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:0.5)
              self.view.backgroundColor = UIColor.black
              view1.contentMode = UIView.ContentMode.scaleAspectFill
          }else if UserDefaults.standard.integer(forKey: "like") == 3{
              self.tabBarController?.tabBar.barTintColor = UIColor(red:0.78, green:0.75, blue:0.90, alpha:1.0)
              self.tabBarController?.tabBar.tintColor = UIColor(red:0.51, green:0.44, blue:0.78, alpha:1.0)
              view1.backgroundColor = UIColor(patternImage: UIImage(named: "ing3")!)
              ansView.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0.5)
              historyButton.backgroundColor = UIColor(red:0.60, green:0.54, blue:0.82, alpha:1.0)
              for button in operatorButtons {
                  button.backgroundColor = UIColor(red:0.60, green:0.54, blue:0.82, alpha:1.0)
              }
              for button in numberButtons {
                  button.backgroundColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:0.5)
              }
              equalButton.backgroundColor = UIColor(red:0.51, green:0.44, blue:0.78, alpha:1.0)
              self.view.backgroundColor = UIColor(red:0.78, green:0.75, blue:0.90, alpha:1.0)
              view1.contentMode = UIView.ContentMode.scaleToFill
          }
      }
  </code></pre>
  
  <p>The above code was my theme function, which I call every time the view appears.
  As you see above, I have tried</p>
  
  <pre class="lang-swift prettyprint-override"><code>backgroundImage.image = nil
  </code></pre>
  
  <p>But it didn't work for me. I have also searched stack-overflow for similar questions, but it was no use. Any help is appreciated</p>