简体   繁体   English

刷新 UIImageView 的图片

[英]Refresh UIImageView's Image

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.我正在使用 Swift 5 和 Xcode 11 制作一个 iOS 选项卡式应用程序。我有一个背景 UIImageView,每次用户按下按钮时都会更改图像。 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:这是我的代码:


    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
        }
    }

The above code was my theme function, which I call every time the view appears.上面的代码是我的主题函数,每次视图出现时我都会调用它。 As you see above, I have tried正如你在上面看到的,我已经尝试过

backgroundImage.image = nil

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任何帮助表示赞赏

In the third condition you forgot to set image or it's intentionally ?在第三种情况下,您忘记设置图像还是故意的? And there is no need to set nil before replacing the new image.并且在替换新图像之前不需要设置 nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM