将页面控件设置为UIPageViewController的顶部

时间:2018-04-07 16:51:43

标签: swift4 uipageviewcontroller pagecontrol

目前我想使用UIPageViewController。但默认情况下,页面控件是屏幕底部的自动位置。那么我怎样才能实现如下页面控件位于顶部的图像

enter image description here

我使用容器视图并将视图控制器更改为uipageviewcontroller

更新

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
}


}

我已经更新了我已经做过的最新代码,但页面控件似乎仍然位于页面底部。

下面是我当前页面控件的图像

enter image description here

1 个答案:

答案 0 :(得分:0)

不要从外观使用默认的UIPageControl。删除那些块。 根据您的视图rect和position创建一个新的UIPageControl。

  

在UIpagecontrol的框架中:设置y = 0表示顶部或   UIScreen.main.bounds.maxY - 偏移量

以下是示例代码

 //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)
}

您需要从 viewControllerAfter ()&amp;更新当前页面(pageControl.currentPage = count)的 viewControllerBefore