Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created August 10, 2022 20:12
Show Gist options
  • Save christianselig/06c48cf7a535e6d7aef7aba19236fcce to your computer and use it in GitHub Desktop.
Save christianselig/06c48cf7a535e6d7aef7aba19236fcce to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIPageViewController, UIPageViewControllerDataSource {
var redBox: UIView!
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()
// Trying to migrate some old frame based code to Auto Layout
redBox = UIView()
redBox.backgroundColor = .systemRed
redBox.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(redBox)
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)
])
let vcB = ViewControllerB()
setViewControllers([vcB], direction: .forward, animated: false)
// NSLayoutConstraint.activate([
// vcB.greenBox.bottomAnchor.constraint(equalTo: redBox.topAnchor)
// ])
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
return ViewControllerB()
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
return ViewControllerB()
}
}
class ViewControllerB: UIViewController {
var greenBox: UIView!
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)
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)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment