3

Need some help. I've implemented a uipagecontrol where the dots are programmatically added. It was just working. I didn't touch the code but I was working on the UI in storyboard. Then I ran it and they've disappeared. I tried using the debugger tool View Hierarchy but no luck. They don't appear there. Could me working on the UI in storyboard trigger something?

import UIKit

class EmployeeTutorialViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

var pageControl = UIPageControl()

// MARK: UIPageViewControllerDataSource

lazy var orderedViewControllers: [UIViewController] = {
    return [self.newVc(viewController: "employee1"),
            self.newVc(viewController: "employee2"),
            self.newVc(viewController: "employee3"),
            self.newVc(viewController: "employee4"),
            self.newVc(viewController: "employee5"),
            self.newVc(viewController: "employee6"),
            self.newVc(viewController: "employee7"),
            self.newVc(viewController: "employee8")]
}()

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self



    // This sets up the first view that will show up on our page control
    if let firstViewController = orderedViewControllers.first {
        setViewControllers([firstViewController],
                           direction: .forward,
                           animated: true,
                           completion: nil)
    }

    configurePageControl()

    // Do any additional setup after loading the view.
}

func configurePageControl() {
    // The total number of pages that are available is based on how many available colors we have.
    pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))


    self.pageControl.numberOfPages = orderedViewControllers.count
    self.pageControl.currentPage = 0
    self.pageControl.tintColor = UIColor.black
    self.pageControl.pageIndicatorTintColor = UIColor.white
    self.pageControl.currentPageIndicatorTintColor = UIColor.gray
    self.view.addSubview(pageControl)
}

func newVc(viewController: String) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}


// MARK: Delegate methords
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    let pageContentViewController = pageViewController.viewControllers![0]
    self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
}

// MARK: Data source functions.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    // User is on the first view controller and swiped left to loop to
    // the last view controller.
    guard previousIndex >= 0 else {
        //return orderedViewControllers.last
        // Uncommment the line below, remove the line above if you don't want the page control to loop.
        return nil
    }

    guard orderedViewControllers.count > previousIndex else {
        return nil
    }

    return orderedViewControllers[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1
    let orderedViewControllersCount = orderedViewControllers.count

    // User is on the last view controller and swiped right to loop to
    // the first view controller.
    guard orderedViewControllersCount != nextIndex else {
        //return orderedViewControllers.first
        // Uncommment the line below, remove the line above if you don't want the page control to loop.
        return nil
    }

    guard orderedViewControllersCount > nextIndex else {
        return nil
    }

    return orderedViewControllers[nextIndex]
}

}
Lukas Bimba
  • 817
  • 14
  • 35
  • 1
    One question: Why are you adding a separate page control when a UIPageViewController _has_ a page control? It might be simpler just to use that. – matt May 27 '18 at 17:35
  • Also, are you aware that you're providing the data source for this page view controller in a completely wrong way? You should not be creating an array of view controllers in advance. You should create and supply each view controller only when asked for it in the page view controller's data source methods. – matt May 27 '18 at 17:37
  • Anyways.... Me changing my navigation controller bar to translucent brought them back????? wtf? – Lukas Bimba May 27 '18 at 17:37
  • is this considered a bug? – Lukas Bimba May 27 '18 at 17:41
  • I don't know what "this" is, exactly, but it sounds fascinating. Can you reduce this to an example project? A project where changing the navigation controller's bar translucency changes the presence of a control in the view... I'd loooove to see that! – matt May 27 '18 at 17:50
  • I created a screen recording – Lukas Bimba May 27 '18 at 17:57
  • How can I send you it – Lukas Bimba May 27 '18 at 17:57
  • We'd need to see a project we can run and test this for ourselves, not a screen recording. You could post it on github or similar... – matt May 27 '18 at 18:04

0 Answers0