简体   繁体   中英

Set page control to the top of UIPageViewController

Currently i want to use UIPageViewController. But by default the page control is automatic place on the bottom of screen. so how can i achieve like the image below where the page control is on the top

在此处输入图片说明

i use container view and change the view controller to uipageviewcontroller

UPDATE

class ProfilePageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

var pageControl = UIPageControl(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 50))
lazy var VCArr: [UIViewController] = {
    return [self.VCInstance(name: "button1"),
            self.VCInstance(name: "button2"),
            self.VCInstance(name: "button3")]
}()

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clear
        }
    }
}

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

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self
    pageControl.currentPageIndicatorTintColor = UIColor.green
    pageControl.pageIndicatorTintColor = UIColor.gray
    if let firstVC = VCArr.first {
        setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    }
}



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

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return VCArr.last
        //uncomment atas comment bawa untuk loop
        //return nil
    }

    guard VCArr.count > previousIndex else {
        return nil
    }

    return VCArr[previousIndex]

}

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

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < VCArr.count else {
        return VCArr.first
        //uncomment atas, comment bawah untuk loop
        //return nil
    }

    guard VCArr.count > nextIndex else {
        return nil
    }

    return VCArr[nextIndex]

}

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


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

    return firstViewControllerIndex
}


}

i have already updated the latest code what i had already did, but the page control seem still at the bottom of page.

below is the image of my current page control

在此处输入图片说明

Don't use the default UIPageControl from appearance. Remove those block. Create a new UIPageControl based on your view rect and position.

In frame of UIpagecontrol: set y = 0 means top or UIScreen.main.bounds.maxY - offset

Here is the sample code

 //1 : member variable
 var pageControl = UIPageControl()

// Call it from viewDidLoad
func setupPageControl() {
   // 2: set y = 0 means top or UIScreen.main.bounds.maxY - offset
    pageControl = UIPageControl(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 50))
    self.pageControl.numberOfPages = pageCount
   // self.pageControl.currentPage = 0
    self.pageControl.tintColor = UIColor.lightGray
    self.pageControl.pageIndicatorTintColor = UIColor.lightGray
    self.pageControl.currentPageIndicatorTintColor = UIColor.black
    pageControl.backgroundColor = UIColor.clear
    self.view.addSubview(pageControl)
}

You need to update the current page ( pageControl.currentPage = count ) from viewControllerAfter () & viewControllerBefore

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM