Advertisement
Don_Mag

Untitled

Apr 20th, 2023
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.26 KB | None | 0 0
  1. //
  2.  
  3. class PagesViewController: UIViewController {
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         // a UIView to hold the page view controller
  9.         let pvcContainer = UIView()
  10.         pvcContainer.backgroundColor = .gray
  11.        
  12.         [pvcContainer].forEach { v in
  13.             v.translatesAutoresizingMaskIntoConstraints = false
  14.             view.addSubview(v)
  15.         }
  16.        
  17.         let g = view.safeAreaLayoutGuide
  18.         NSLayoutConstraint.activate([
  19.            
  20.             pvcContainer.topAnchor.constraint(equalTo: g.topAnchor, constant: 100.0),
  21.             pvcContainer.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  22.             pvcContainer.widthAnchor.constraint(equalToConstant: 200.0),
  23.             pvcContainer.heightAnchor.constraint(equalTo: pvcContainer.widthAnchor, multiplier: 1.5),
  24.            
  25.         ])
  26.        
  27.         let pvc = MyPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal)
  28.         addChild(pvc)
  29.  
  30.         pvc.view.translatesAutoresizingMaskIntoConstraints = false
  31.         pvcContainer.addSubview(pvc.view)
  32.        
  33.         NSLayoutConstraint.activate([
  34.            
  35.             pvc.view.topAnchor.constraint(equalTo: pvcContainer.topAnchor),
  36.             pvc.view.leadingAnchor.constraint(equalTo: pvcContainer.leadingAnchor),
  37.             pvc.view.trailingAnchor.constraint(equalTo: pvcContainer.trailingAnchor),
  38.             pvc.view.bottomAnchor.constraint(equalTo: pvcContainer.bottomAnchor),
  39.            
  40.         ])
  41.        
  42.         pvc.didMove(toParent: self)
  43.        
  44.     }
  45.    
  46. }
  47.  
  48. class MyPageViewController: UIPageViewController {
  49.    
  50.     // so we can get the index of the current page
  51.     var currentIndex: Int {
  52.         guard let vc = viewControllers?.first else { return 0 }
  53.         return pageViewControllers.firstIndex(of: vc) ?? 0
  54.     }
  55.    
  56.     let colors: [UIColor] = [
  57.         .systemRed,
  58.         .systemGreen,
  59.         .systemBlue,
  60.         .systemYellow,
  61.         .green,
  62.         .blue,
  63.     ]
  64.    
  65.     var pageViewControllers: [UIViewController] = [UIViewController]()
  66.    
  67.     override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
  68.         super.init(transitionStyle: style, navigationOrientation: navigationOrientation, options: options)
  69.     }
  70.    
  71.     required init?(coder: NSCoder) {
  72.         super.init(coder: coder)
  73.     }
  74.    
  75.     override func viewDidLoad() {
  76.         super.viewDidLoad()
  77.        
  78.         dataSource = self
  79.        
  80.         // instantiate all "pages"
  81.        
  82.         for i in 0..<colors.count {
  83.             let vc = ExamplePageVC()
  84.             vc.theLabel.text = "Page: \(i)"
  85.             vc.view.backgroundColor = colors[i]
  86.             pageViewControllers.append(vc)
  87.         }
  88.        
  89.         setViewControllers([pageViewControllers[0]], direction: .forward, animated: false, completion: nil)
  90.     }
  91.    
  92. }
  93.  
  94. extension MyPageViewController: UIPageViewControllerDataSource {
  95.     func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  96.        
  97.         guard let viewControllerIndex = pageViewControllers.firstIndex(of: viewController) else { return nil }
  98.        
  99.         let previousIndex = viewControllerIndex - 1
  100.        
  101.         // if we are at the First controller,
  102.         //  return nil
  103.         guard previousIndex >= 0 else { return nil }
  104.        
  105.         return pageViewControllers[previousIndex]
  106.     }
  107.    
  108.     func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  109.        
  110.         guard let viewControllerIndex = pageViewControllers.firstIndex(of: viewController) else { return nil }
  111.        
  112.         let nextIndex = viewControllerIndex + 1
  113.        
  114.         // if we are at the Last controller,
  115.         //  return nil
  116.         guard nextIndex < pageViewControllers.count else { return nil }
  117.        
  118.         return pageViewControllers[nextIndex]
  119.     }
  120.    
  121. }
  122.  
  123. class ExamplePageVC: UIViewController {
  124.    
  125.     let theLabel: UILabel = {
  126.         let v = UILabel()
  127.         v.translatesAutoresizingMaskIntoConstraints = false
  128.         v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
  129.         v.textAlignment = .center
  130.         return v
  131.     }()
  132.    
  133.     override func viewDidLoad() {
  134.         super.viewDidLoad()
  135.         view.backgroundColor = .blue
  136.        
  137.         view.addSubview(theLabel)
  138.         NSLayoutConstraint.activate([
  139.             theLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  140.             theLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
  141.             theLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8),
  142.             theLabel.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.8),
  143.         ])
  144.        
  145.     }
  146.    
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement