使用UIPageViewController进行Swift容器视图

时间:2018-04-20 05:56:01

标签: ios swift uipageviewcontroller uicontainerview

我有一个显示UIPageViewController的容器视图。一切正常,唯一的问题是显示页面的点。它们无法根据不断变化的屏幕尺寸进行调整。

pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 525,width: UIScreen.main.bounds.width,height: 50))

这是我用来改变点的显示位置,主要是UIScreen.main.bounds.maxY - 525部分。我只是在检查如何让它们适应不同的屏幕尺寸。屏幕截图下面的问题。第一个橙色屏幕在我想要点的位置是正确的,第二个是错误的。

enter image description here

enter image description here

更多代码

 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 - 525,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.black
    self.view.addSubview(pageControl)
}

我如何调用观看次数

lazy var orderedViewControllers: [UIViewController] = {
    return [self.newVc(viewController: "sbBlue"),
            self.newVc(viewController: "sbRed")]
}()

2 个答案:

答案 0 :(得分:0)

以下是您的问题的解决方案。

您可以获得绿色或橙色视图的maxY位置,并按照以下方式更改代码。它将通过从绿色或橙色视图底部删除30像素来设置pageControl的垂直位置。

pageControl = UIPageControl(frame: CGRect(x: 0, y: yourOrangeView.bounds.maxY - 30, width: UIScreen.main.bounds.width, height: 50))

或者

pageControl = UIPageControl(frame: CGRect(x: 0, y: yourGreenView.bounds.maxY - 30, width: UIScreen.main.bounds.width, height: 50))

我希望这可以解决您的问题。

答案 1 :(得分:0)

这里有两个问题。

  • 您不应该创建自己的UIPageControl。 UIPageViewController 已经一个UIPageControl;你应该使用它。

  • 如果您坚持提供自己的(不必要的)UIPageControl,则需要使用自动布局进行定位,以便考虑尺寸变化。这段代码错了:

    pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 525,width: UIScreen.main.bounds.width,height: 50))
    

    不要给页面控件一个框架;给它自动布局约束。