如何为UIViewController提供静态背景图像?

时间:2019-04-06 21:52:52

标签: swift

我正在尝试将以下代码插入我的viewDidLoad中,该应用将以图案类型视图显示该图像。我正在尝试找到一种使图像适合整个屏幕的方法。

我没有做太多尝试,因为我是Swift的新手。

view.backgroundColor = UIColor(patternImage: UIImage(named: "GasBackground.png")!)

1 个答案:

答案 0 :(得分:0)

考虑添加一个UIImageView作为根视图的子视图:

// Creating UIImageView, which holds an image
let imageView = UIImageView()
imageView.image = UIImage(named: "GasBackground.png")
imageView.contentMode = .scaleAspectFill

// Adding the image view to the view hierarchy
view.addSubview(imageView)

// Make image view to fit entire screen
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
  imageView.topAnchor.constraint(equalTo: view.topAnchor),
  imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])