首页 > 解决方案 > 我无法设置背景图片(Swift)

问题描述

我做了一个简单的计时器应用程序,我想在后台添加背景图片或视频。但是,我不能这样做,我不知道为什么。

谁能告诉我为什么图片没有出现在背景中?viewDidLoad我在方法中设置了背景。也许@IBOutlet var backgroundView: UIView!部分是错误的?UIView当我制作这个 Xcode 项目时,我将它与给定的连接起来。

class ViewController: UIViewController, UITableViewDelegate {
    let mainStopwatch = Stopwatch()
    let lapStopwatch = Stopwatch()
    var isPlay: Bool = false

    var laps: [String] = []
    //UI components

    @IBOutlet weak var lapRestButton: UIButton!
    @IBOutlet weak var playPauseButton: UIButton!
    @IBOutlet weak var lapTimerLabel: UILabel!
    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var lapsTableView: UITableView!
    @IBOutlet var backgroundView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addBackground(name:  "nathan-dumlao-iXXyrEwZgiU-unsplash")

        lapRestButton.isEnabled = false

        lapsTableView.dataSource = self
        lapsTableView.delegate = self
    }

    //UI Settings
    override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }

    //Actions
    @IBAction func playPauseTimer(_ sender: Any) {
        lapRestButton.isEnabled = true
        changeButton(lapRestButton, title: "Lap", color: .black)
    }
}

这是另一个用于设置背景图片的 swift 文件。

extension UIView {

func addBackground(name: String) {
    let width = UIScreen.main.bounds.size.width
    let height = UIScreen.main.bounds.size.height

    let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
    imageViewBackground.image = UIImage(named: name)

    imageViewBackground.contentMode = .scaleAspectFill

    self.addSubview(imageViewBackground)
    self.sendSubviewToBack(imageViewBackground)
}

标签: swiftxcode

解决方案


var timer = Timer()
var counter = 0   // the counter is use for the index of array in below code
var imageArr = ["image","image1","image2"] //Image arr 

override func viewDidLoad() {
    super.viewDidLoad()

    self.addBackground(name: "image") // set default image on your view

    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) // timer will start and it will change view background image after one min you can change time according to your requirement.
}

 // called every time interval from the timer
  @objc func timerAction() {
    self.addBackground(name: self.imageArr[counter]) // here we are change our view background image when timer function is run on every min
    counter += 1 // every-time counter will increment by one so the image array index will also increment and we will get our new image
   }

  // that's your code only you need to change one line that last one
   func addBackground(name: String) {
    let width = UIScreen.main.bounds.size.width
    let height = UIScreen.main.bounds.size.height

    let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
    imageViewBackground.image = UIImage(named: name)

    imageViewBackground.contentMode = .scaleAspectFit

    self.viewref.addSubview(imageViewBackground) // here we will set the image view to our view 
}

希望这将帮助您改变您的视图背景。谢谢。


推荐阅读