我可以像这样更改 viewDidLoad 函数的背景图片:
let myBackgroundImage = UIImageView (frame: UIScreen.main.bounds)
myBackgroundImage.image = UIImage(named: "wallpaper-1-iphone-8-plus.png")
myBackgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(myBackgroundImage, at: 0)
我想用这样的按钮点击做同样的 Action :
@IBAction func flip4btn5(_ sender: Any)
{
let myBackgroundImage = UIImageView (frame: UIScreen.main.bounds)
myBackgroundImage.image = UIImage(named: "wallpaper-2-iphone-8-plus.png")
myBackgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(myBackgroundImage, at: 0)
}
但它不会改变背景图像。为什么 ?你怎么看?
我正在使用 Swift 4.1。
最佳答案
不要创建新图像。只需像这样更改按钮操作中第一个 UIImage 的图片:
myBackgroundImage.image = UIImage(named: "wallpaper-2-iphone-8-plus.png")
关于ios - 单击按钮更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49876214/