Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created August 11, 2022 16:21
Show Gist options
  • Save christianselig/af90a7bb99a6e772363da9ca6efe3485 to your computer and use it in GitHub Desktop.
Save christianselig/af90a7bb99a6e772363da9ca6efe3485 to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIPageViewController, UIPageViewControllerDataSource {
var redBox: UIView!
var specialLayoutGuide: UILayoutGuide!
init() {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal)
dataSource = self
}
required init?(coder aDecoder: NSCoder) { fatalError("\(#file) does not implement coder.") }
override func viewDidLoad() {
super.viewDidLoad()
redBox = UIView()
redBox.backgroundColor = .systemRed
redBox.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(redBox)
specialLayoutGuide = UILayoutGuide()
view.addLayoutGuide(specialLayoutGuide)
NSLayoutConstraint.activate([
redBox.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50.0),
redBox.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50.0),
redBox.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
redBox.heightAnchor.constraint(equalToConstant: 50.0),
specialLayoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
specialLayoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),
specialLayoutGuide.topAnchor.constraint(equalTo: redBox.topAnchor),
])
let vcB = ViewControllerB(specialLayoutGuide: specialLayoutGuide)
setViewControllers([vcB], direction: .forward, animated: false)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
return nil
}
}
class ViewControllerB: UIViewController {
var greenBox: UIView!
let specialLayoutGuide: UILayoutGuide
init(specialLayoutGuide: UILayoutGuide) {
self.specialLayoutGuide = specialLayoutGuide
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) { fatalError("\(#file) does not implement coder.") }
override func viewDidLoad() {
super.viewDidLoad()
let randomWhite = CGFloat((0 ... 100).randomElement()!) / 100.0
view.backgroundColor = UIColor(white: randomWhite, alpha: 1.0)
greenBox = UIView()
greenBox.backgroundColor = .systemGreen
greenBox.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(greenBox)
}
override func updateViewConstraints() {
super.updateViewConstraints()
NSLayoutConstraint.activate([
greenBox.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 50.0),
greenBox.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50.0),
greenBox.heightAnchor.constraint(equalToConstant: 50.0),
greenBox.bottomAnchor.constraint(equalTo: specialLayoutGuide.topAnchor)
])
}
}
@christianselig
Copy link
Author

Resulting error:

Thread 1: "The layout constraints still need update after sending -updateViewConstraints to <TestChildVC.ViewControllerB: 0x139d09c00>.\nTestChildVC.ViewControllerB or one of its superclasses may have overridden -updateViewConstraints without calling super or sending -updateConstraints to the view. Or, something may have dirtied layout constraints in the middle of updating them. Both are programming errors."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment