问题描述
我有一个 UIPageViewController
的子类,并在其 viewDidLoad中
我要添加的UILabel
到 self.view
。这工作得很好,如果我设置使用框架的地位,但如果我尝试使用自动布局来定位它,我得到:
I have a UIPageViewController
subclass, and in its viewDidLoad
I want to add a UILabel
to its self.view
. This works fine if I set its position using frames, but if I try to position it using Auto Layout, I get:
的 * 的中断言失败 - [_ UIPageViewControllerContentView layoutSublayersOfLayer:],/SourceCache/UIKit_Sim/UIKit-2935.58/UIView.m:8742
我应该怎么定位这个中的UILabel显示了 UIPageViewController
?
How am I supposed to position this UILabel to show up in the UIPageViewController
?
推荐答案
这似乎发生,因为 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.
这篇关于我该如何定位使用自动布局UIPageViewController子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!