Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- class PagesViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // a UIView to hold the page view controller
- let pvcContainer = UIView()
- pvcContainer.backgroundColor = .gray
- [pvcContainer].forEach { v in
- v.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(v)
- }
- let g = view.safeAreaLayoutGuide
- NSLayoutConstraint.activate([
- pvcContainer.topAnchor.constraint(equalTo: g.topAnchor, constant: 100.0),
- pvcContainer.centerXAnchor.constraint(equalTo: g.centerXAnchor),
- pvcContainer.widthAnchor.constraint(equalToConstant: 200.0),
- pvcContainer.heightAnchor.constraint(equalTo: pvcContainer.widthAnchor, multiplier: 1.5),
- ])
- let pvc = MyPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal)
- addChild(pvc)
- pvc.view.translatesAutoresizingMaskIntoConstraints = false
- pvcContainer.addSubview(pvc.view)
- NSLayoutConstraint.activate([
- pvc.view.topAnchor.constraint(equalTo: pvcContainer.topAnchor),
- pvc.view.leadingAnchor.constraint(equalTo: pvcContainer.leadingAnchor),
- pvc.view.trailingAnchor.constraint(equalTo: pvcContainer.trailingAnchor),
- pvc.view.bottomAnchor.constraint(equalTo: pvcContainer.bottomAnchor),
- ])
- pvc.didMove(toParent: self)
- }
- }
- class MyPageViewController: UIPageViewController {
- // so we can get the index of the current page
- var currentIndex: Int {
- guard let vc = viewControllers?.first else { return 0 }
- return pageViewControllers.firstIndex(of: vc) ?? 0
- }
- let colors: [UIColor] = [
- .systemRed,
- .systemGreen,
- .systemBlue,
- .systemYellow,
- .green,
- .blue,
- ]
- var pageViewControllers: [UIViewController] = [UIViewController]()
- override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
- super.init(transitionStyle: style, navigationOrientation: navigationOrientation, options: options)
- }
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- dataSource = self
- // instantiate all "pages"
- for i in 0..<colors.count {
- let vc = ExamplePageVC()
- vc.theLabel.text = "Page: \(i)"
- vc.view.backgroundColor = colors[i]
- pageViewControllers.append(vc)
- }
- setViewControllers([pageViewControllers[0]], direction: .forward, animated: false, completion: nil)
- }
- }
- extension MyPageViewController: UIPageViewControllerDataSource {
- func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
- guard let viewControllerIndex = pageViewControllers.firstIndex(of: viewController) else { return nil }
- let previousIndex = viewControllerIndex - 1
- // if we are at the First controller,
- // return nil
- guard previousIndex >= 0 else { return nil }
- return pageViewControllers[previousIndex]
- }
- func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
- guard let viewControllerIndex = pageViewControllers.firstIndex(of: viewController) else { return nil }
- let nextIndex = viewControllerIndex + 1
- // if we are at the Last controller,
- // return nil
- guard nextIndex < pageViewControllers.count else { return nil }
- return pageViewControllers[nextIndex]
- }
- }
- class ExamplePageVC: UIViewController {
- let theLabel: UILabel = {
- let v = UILabel()
- v.translatesAutoresizingMaskIntoConstraints = false
- v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
- v.textAlignment = .center
- return v
- }()
- override func viewDidLoad() {
- super.viewDidLoad()
- view.backgroundColor = .blue
- view.addSubview(theLabel)
- NSLayoutConstraint.activate([
- theLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
- theLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
- theLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8),
- theLabel.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.8),
- ])
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement