Hide dots on final page of UIPageViewController swift

后端 未结 5 1534
小鲜肉
小鲜肉 2021-01-16 08:59

I have four pages in a UIPageViewController, and I\'d like to hide the dots on the last page. I successfully made a function that gets called on the last page of the UIPageV

相关标签:
5条回答
  • 2021-01-16 09:19

    Check out the accepted answer here to get the UIPageControl (the view with the dots):

    Access the UIPageControl created by iOS6 UIPageViewController?

    Then, once you have the pageControl, in pageViewController: didFinishAnimating: you can check if it's the last page, and if so set pageControl.hidden = true.

    0 讨论(0)
  • 2021-01-16 09:28

    use the UIPageViewControllerDelegate method:

    let pageControl = UIPageControl()
    
    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if (index == numberOfPages()-1){
              pageControl.hidden = true
        }else{
              pageControl.hidden = false
        }
    }
    
    0 讨论(0)
  • 2021-01-16 09:37

    I found that you can't actually hide the page control as it just never works. But a bit of a hack is to add this to your code:

        func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
            if let lastVC = pendingViewControllers.last?.isKindOfClass(youLastViewController){
                if lastVC == true {
                    for view in self.view.subviews {
                        if view is UIScrollView {
                            view.frame = UIScreen.mainScreen().bounds
                        } else if view is UIPageControl {
                            view.center.y = self.view.frame.height * 1.5
                        }
                    }
                }else {
                    for view in self.view.subviews {
                        if view is UIScrollView {
                            view.frame = UIScreen.mainScreen().bounds
                        } else if view is UIPageControl {
                            view.center.y = self.view.frame.height * 0.92
                        }
                    }
                }
            }
        }
    }
    

    This actually move the page control of the screen and the same as hiding it.

    0 讨论(0)
  • 2021-01-16 09:43
    override func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        if /*current index is the last one*/ {
            return 0
        } else {
            /*return number of pages*/
        }
    }
    
    0 讨论(0)
  • 2021-01-16 09:44

    here is the complete code of UIPageViewController to hide dots from last page it's work fine

    first create class for UIViewController and add UIPageViewControllerDelegate , UIPageViewControllerDataSource

    create outlet for pageControl

    @IBOutlet weak var pageControl: UIPageControl!

    create variable for uipagecontroller

    var pageControllerContainer : UIPageViewController!
    
    var pages = [UIViewController]()
    
    var currentIndex : Int?
    
    private var pendingIndex : Int?
    private var lastIndex : Int?
    

    in ViewDidLoad: add following lines :

     let page1 : UIViewController! = self.storyboard?.instantiateViewController(withIdentifier: "YourFirstPageViewController") as! YourFirstPageViewController
        let page2 : UIViewController! = self.storyboard?.instantiateViewController(withIdentifier: "YourSecondPageViewController") as! YourSecondPageViewController
        let page3 : UIViewController! = self.storyboard?.instantiateViewController(withIdentifier: "YourThirdPageViewController") as! YourThirdPageViewController
    
        pages.append(page1)
        pages.append(page2)
        pages.append(page3)
    
        pageControllerContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        pageControllerContainer.delegate = self
        pageControllerContainer.dataSource = self
        pageControllerContainer.setViewControllers([page1], direction: .forward, animated: true, completion: nil)
    
        view.addSubview(pageControllerContainer.view)
        view.bringSubview(toFront: pageControl)
        pageControl.numberOfPages = pages.count
        pageControl.currentPage = 0
    

    now create following delegates method for pageviewcontroller:

         func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    
        let currentIndex = pages.index(of: viewController)
    
        if currentIndex == 0
        {
            return nil
        }
        let previousIndex = abs((currentIndex! - 1) % pages.count)
        return pages[previousIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    
    
        let currentIndex = pages.index(of: viewController)
    
        if currentIndex == pages.count - 1
        {
            return nil
        }
    
        let nextIndex = abs((currentIndex! + 1 ) % pages.count)
        return pages[nextIndex]
    }
    

    after that create following datasource methods

        func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    
        pendingIndex = pages.index(of: pendingViewControllers.first!)
    
    }
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    
        if completed
        {
    
            currentIndex = pendingIndex
            if let index = currentIndex
            {
                pageControl.currentPage = index
            }
        }
         lastIndex = pages.count - 1
          print(lastIndex as Any)
          print(currentIndex as Any)
    
        if currentIndex == lastIndex
        {
            pageControl.isHidden = true
        }
        else
        {
            pageControl.isHidden = false
        }
    }
    

    this will allows to hide page controller when it comes to last index means last page

    hope this will help :-) thank you ...

    0 讨论(0)
提交回复
热议问题