我想在viewDidLoad中设置默认背景为:

override func viewDidLoad() {
    super.viewDidLoad()

    let bkgrnd1 = UIImageView(frame: UIScreen.main.bounds)
    bkgrnd1.image = UIImage(named: "bkgrnd1.png")
    bkgrnd1.contentMode = UIView.ContentMode.scaleAspectFill
    self.view.insertSubview(bkgrnd1, at: 0)
}

之后,我想再有2个单独的UIButton来替换和/或停用默认的“bkgrnd1”和另一个“bkgrnd2.png”。

最佳答案

您需要将bkgrnd1设置为实例变量以在vc的所有函数中访问它,因为它目前是本地变量

var bkgrnd1 : UIImageView!

  override func viewDidLoad() {
        super.viewDidLoad()

      bkgrnd1 = UIImageView(frame: UIScreen.main.bounds)
      bkgrnd1.image = UIImage(named: "bkgrnd1.png")
      bkgrnd1.contentMode = UIView.ContentMode.scaleAspectFill
      self.view.insertSubview(bkgrnd1, at: 0)
}

然后做
bkgrnd1.image = UIImage(named: "bkgrnd2.png")

任何地方,如果要隐藏设置
bkgrnd1.isHiddden = true

10-08 08:11