【问题标题】:Swift Container View with UIPageViewController带有 UIPageViewController 的 Swift 容器视图
【发布时间】:2018-04-20 05:56:01
【问题描述】:

我有一个显示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 部分。我只是在检查如何让它们适应不同的屏幕尺寸。下面是问题的屏幕截图。第一个橙色屏幕在我想要点的位置上是正确的,而第二个是错误的。

更多代码

 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")]
}()

【问题讨论】:

  • 查看约束
  • 它以编程方式设置,我添加了更多代码
  • 您是否引用了希望 PageControl 调整到的视图? (即橙色或绿色视图)
  • @LukasBimba,你想为任何屏幕尺寸调整垂直位置,对吗?
  • 我正在使用情节提要 ID 调用视图,也许我只需要找出另一种方法来完成我想要的外观?

标签: ios swift uipageviewcontroller uicontainerview


【解决方案1】:

这是您的问题的解决方案。

您可以获得绿色或橙色视图的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))

我希望这能解决您的问题。

【讨论】:

  • 我没有我的“红色”和“橙色”视图作为带有容器视图的视图控制器中的 IBOutlets。我应该尝试添加它们吗?我通过“Storyboard ID”调用视图,我将添加代码...
  • 然后,您可以使用相同的插座。它将解决您的问题。
【解决方案2】:

这里有两个问题。

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

  • 如果你坚持提供你自己的(不必要的)UIPageControl,你需要用自动布局来定位它,以便它考虑到大小的变化。这段代码是错误的:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2014-12-29
    • 1970-01-01
    相关资源
    最近更新 更多