且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我该如何定位使用自动布局UIPageViewController子视图?

更新时间:2023-12-05 20:35:10

这似乎发生,因为 UIPageViewController 的观点(这实际上是一个如预期(至少在iOS的7 _UIPageViewControllerContentView )不处理子视图和自动布局;它的iOS 8)工作对我罚款

This seems to be happening because UIPageViewController's view (which is actually a _UIPageViewControllerContentView) doesn't handle subviews and autolayout as expected (at least in iOS 7; it worked fine for me in iOS 8).

您可以通过不使用自动布局解决这个问题,但我想用自动布局,所以我结束了重做我的视图控制器,以避免增加子视图 UIPageViewController

You can get around this by not using autolayout, but I wanted to use autolayout so I ended up redoing my view controllers to avoid adding subviews to UIPageViewController.

而不是子类化 UIPageViewController ,我创建了一个容器视图控制器(子类的UIViewController )以 UIPageViewController 作为一个子视图控制器。那时,我最初曾在 UIPageViewController 自定义子视图被添加到容器的视图。我还做了容器中的数据源和委托的 UIPageViewController

Instead of subclassing UIPageViewController, I created a container view controller (a subclass of UIViewController) with a UIPageViewController as a child view controller. The custom subviews that I initially had in UIPageViewController were then added to the container's view. I also made the container the data source and delegate for the UIPageViewController.

有一个相关话题,人们得到了相同的断言失败,但与的UITableViewCell :"Auto布局执行-layoutSubviews&QUOT后仍然需要;用的UITableViewCell子类。大部分的建议有没有工作或不适用UIPageViewController,但它帮助我弄清楚为什么添加子视图与自动布局可能会导致问题。

There's a related thread where people got the same assertion failure but with a UITableViewCell: "Auto Layout still required after executing -layoutSubviews" with UITableViewCell subclass. Most of the suggestions there don't work or aren't applicable to UIPageViewController, but it helped me figure out why adding subviews with autolayout might cause a problem.