ios - 单击按钮更改背景图像

标签 ios swift uiimageview

我可以像这样更改 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/

相关文章:

ios - 父 View iOS 的 subview 约束

iphone - 在设备上运行收到 "Finish running on device"消息但从未运行

ios - 从Core-Data请求不同的NSManagedObject属性实例时返回什么类?

swift - 使用三个数组填充复杂时间线

ios - 我应该在渲染到屏幕之前调整 UIImage 的大小吗?

ios - 如何映射一个我不知道参数个数的json

fonts - 如何使用 Swift 创建属性字符串?

ios - 部分屏幕中的 UIScrollView

swift - 在新的 collectionViewCell 中加载 selectedIndexItem(使 imageCache 无效)

objective-c - iOS:TapGestureRecognizer 问题