Created
August 10, 2022 20:42
-
-
Save christianselig/a52924d22d5badbedf32abb9e9949611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
redBox = UIView() | |
redBox.backgroundColor = .systemRed | |
view.addSubview(redBox) | |
let vcB = ViewControllerB(holderViewController: self) | |
setViewControllers([vcB], direction: .forward, animated: false) | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
let sideInset = 50.0 | |
let height = 50.0 | |
redBox.frame = CGRect(x: sideInset, y: view.bounds.height - view.safeAreaInsets.bottom - height, width: view.bounds.width - sideInset * 2.0, height: height) | |
} | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { | |
return ViewControllerB(holderViewController: self) | |
} | |
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { | |
return ViewControllerB(holderViewController: self) | |
} | |
} | |
class ViewControllerB: UIViewController { | |
var greenBox: UIView! | |
private(set) weak var holderViewController: ViewController? | |
init(holderViewController: ViewController) { | |
self.holderViewController = holderViewController | |
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 | |
view.addSubview(greenBox) | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
let sideInset = 50.0 | |
let height = 50.0 | |
greenBox.frame = CGRect(x: sideInset, y: holderViewController!.redBox.frame.origin.y - height, width: view.bounds.width - sideInset * 2.0, height: height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment