如何使用自动布局(约束)将子视图添加到UIPageViewController?

时间:2018-08-21 10:02:38

标签: ios uiimageview uikit uipageviewcontroller

我尝试将UIImageView添加到UIPageVC背景视图中,但是它将作为子视图,因为它将受约束而具有自动布局。

我试图在UIPageViewController类中做类似的事情:

override func viewDidLoad() {
    super.viewDidLoad()
    dataSource = self
    delegate = self

    // Adding an UIImageView to background

    let imageView = UIImageView(image: UIImage(named: "backgoundImage"))
    imageView!.contentMode = .scaleToFill
    view.insertSubview(imageView!, at: 0)
}

感谢所有答案!

1 个答案:

答案 0 :(得分:1)

您需要对广告进行此类限制。

if let imageView = UIImageView(image: UIImage(named: "backgoundImage")) {
  imageView.contentMode = .scaleToFill
  view.insertSubview(imageView, at: 0)

  imageView.translatesAutoresizingMaskIntoConstraints = false

  imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
  imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
  imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
}