Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode Page View Controller: make dots transparent

Tags:

xcode

i have added a page view controller and a 3 pages. I set Transition Style to Scroll and implemented the presentationCount and the presentationIndex method. now i get a black bar with gray/white dots at the bottom of my view. However, but i want the view to goright to the bottom and the make the dots appear over it(without the black background. How do i do that?

here is my code:

import UIKit

class FilterViewController: UIPageViewController, UIPageViewControllerDataSource {



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

        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        }


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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private(set) lazy var orderedViewControllers: [UIViewController] = {
        return [self.newViewController(category: "first"),
                self.newViewController(category: "second"),
                self.newViewController(category: "third")]
    }()

    private func newViewController(category: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil) .
            instantiateViewController(withIdentifier: "\(category)ViewController")
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return orderedViewControllers.last
        }

        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

        guard orderedViewControllersCount != nextIndex else {
            return orderedViewControllers.first
        }

        guard orderedViewControllersCount > nextIndex else {
            return nil
        }

        return orderedViewControllers[nextIndex]
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return orderedViewControllers.count
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        guard let firstViewController = viewControllers?.first,
            let firstViewControllerIndex = orderedViewControllers.index(of: firstViewController) else {
                return 0
        }

        return firstViewControllerIndex
    }


}

page view controller

like image 945
Ginso Avatar asked Jul 27 '18 11:07

Ginso


1 Answers

ok, i got it myself. If anyone has the same problem, just override the viewDidLayoutSubviews() of the Page View Controller:

override func viewDidLayoutSubviews() {
        let v = self.view
        let subviews = v?.subviews
        if subviews?.count == 2 {
            var sv:UIScrollView?
            var pc:UIPageControl?
            for t in subviews! {
                if t is UIScrollView {
                    sv = t as! UIScrollView
                } else {
                    pc = t as! UIPageControl
                }
            }
            if(sv != nil && pc != nil) {
                sv?.frame = (v?.bounds)!
                v?.bringSubview(toFront: pc!)
            }
        }
        super.viewDidLayoutSubviews()
    }
like image 99
Ginso Avatar answered Oct 30 '22 18:10

Ginso