图像视图未出现在屏幕上

Image view doesn't appear on the screen

我正在尝试将背景图片添加到我的屏幕上并设置一些限制,但不幸的是图片视图没有出现,我不知道是什么原因。我检查了图像文件是否在项目文件夹中。

import UIKit

class ViewController: UIViewController {

    let backgroundImageView = UIImageView()

    override func viewDidLoad() {
       super.viewDidLoad()
       setBackground()
    }

    func setBackground() {
        view.addSubview(backgroundImageView)
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        backgroundImageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        backgroundImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        backgroundImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        backgroundImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        backgroundImageView.image = UIImage(named: "background-test-image")
    }

}

将您的图像放入 Assets.xcassets 文件夹