Dmytrii G.
Dmytrii G.

Reputation: 537

How add subviews to UIPageViewController with autolayout ( constraints)?

I try to add the UIImageView to UIPageVC background view, but it will be as subview, because it will have an auto layout by constraints.

I tried to do something like this in UIPageViewController class:

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)
}

Thanks for all answers!

Upvotes: 1

Views: 398

Answers (1)

Satish
Satish

Reputation: 2043

You need to do something like this to ad constraints.

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
}

Upvotes: 1

Related Questions