ios - 如何使用自动布局在 UIPageViewController 中定位 subview ?

标签 ios objective-c uiview autolayout uipageviewcontroller

我有一个 UIPageViewController 子类,在它的 viewDidLoad 中我想添加一个 UILabel 到它的 self.view。如果我使用框架设置它的位置,这很好用,但如果我尝试使用自动布局定位它,我得到:

* Assertion failure in -[_UIPageViewControllerContentView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.58/UIView.m:8742

我应该如何定位这个 UILabel 以显示在 UIPageViewController 中?

最佳答案

这似乎是因为 UIPageViewController 的 View (实际上是一个 _UIPageViewControllerContentView)没有按预期处理 subview 和自动布局(至少在 iOS 7 中是这样;它在 iOS 8 中对我来说运行良好)。

你可以通过不使用自动布局来解决这个问题,但我想使用自动布局,所以我最终重做了我的 View Controller 以避免向 UIPageViewController 添加 subview 。

我没有创建 UIPageViewController 的子类,而是创建了一个容器 View Controller (UIViewController 的子类),并将 UIPageViewController 作为 subview Controller 。我最初在 UIPageViewController 中拥有的自定义 subview 随后被添加到容器的 view 中。我还使容器成为 UIPageViewController 的数据源和委托(delegate)。

有一个相关的线程,人们遇到了同样的断言失败,但是有一个 UITableViewCell:"Auto Layout still required after executing -layoutSubviews" with UITableViewCell subclass .那里的大多数建议都不起作用或不适用于 UIPageViewController,但它帮助我弄清楚了为什么使用自动布局添加 subview 可能会导致问题。

关于ios - 如何使用自动布局在 UIPageViewController 中定位 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21818091/

相关文章:

ios - XCTest UI 测试 : why does my attempt to swipe the last table cell upwards instead slide up the brightness and volume panel?

ios - iOS 中 UINavigationBar 中的 "Back"按钮对 View 有何作用?

iOS:如何获取刚刚在界面生成器中设置的 UIView 的框架

ios - 回答我自己 - uitableview - ios7 - cellForRowAtIndexPath 未调用

objective-c - 同一主机的最大并发 NSURLConnections 数?

ios - 无法理解我的 spritekit 游戏中的延迟?

php - 如何一一管理上传操作

ios - 如何在 Swift 中为我的标签添加轮廓?

ios - 自定义 UICollectionViewCell 属性在出队后为空

ios - SKShapeNode.fillColor 不等于原始 UIColor