使用 UIScrollView 在 UIView layoutSublayersOfLayer 中断言失败
Posted
技术标签:
【中文标题】使用 UIScrollView 在 UIView layoutSublayersOfLayer 中断言失败【英文标题】:Assertion failure in UIView layoutSublayersOfLayer using UIScrollView 【发布时间】:2015-07-30 18:42:49 【问题描述】:我已经制作了一个使用 UIScrollView 在 Swift 中的 UIViewControllers 之间滚动的应用程序,它在 ios8 上运行良好,但是当我在 IOS 7 上运行它时,我遇到了一个巨大的错误,如下所示:
2015-07-30 21:27:05.768 mymessages[40668:60b] *** Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.137/UIView.m:8803
2015-07-30 21:27:05.770 mymessages[40668:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after sending -viewDidLayoutSubviews to the view controller. mymessages.MainViewController's implementation needs to send -layoutSubviews to the view to invoke auto layout.'
我的观点如下:
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
initScrollView()
func initScrollView()
viewController1 = storyboard?.instantiateViewControllerWithIdentifier("firststoryboard") as! controller1
viewController1.willMoveToParentViewController(self)
viewController1.view.frame = scrollView.bounds
viewController2 = storyboard?.instantiateViewControllerWithIdentifier("secondstoryboard") as! controller2
viewController2.willMoveToParentViewController(self)
viewController2.view.frame.size = scrollView.frame.size
viewController2.view.frame.origin = CGPoint(x: view.frame.width, y: 0)
viewController3 = storyboard?.instantiateViewControllerWithIdentifier("thirdstoryboard") as! controller3
viewController3.willMoveToParentViewController(self)
viewController3.view.frame.size = scrollView.frame.size
viewController3.view.frame.origin = CGPoint(x: view.frame.width * 2, y: 0)
scrollView.contentSize = CGSize(width: 3 * scrollView.frame.width, height: scrollView.frame.height)
scrollView.addSubview(viewController3.view)
self.addChildViewController(viewController3)
viewController3.didMoveToParentViewController(self)
scrollView.addSubview(viewController2.view)
self.addChildViewController(viewController2)
viewController2.didMoveToParentViewController(self)
scrollView.addSubview(viewController1.view)
self.addChildViewController(viewController1)
viewController1.didMoveToParentViewController(self)
任何人都解决了这个问题,我不知道如何处理这个错误!
【问题讨论】:
【参考方案1】:多次调用viewDidLayoutSubviews
在 viewDidLayoutSubviews() 中试试这个代码
scrollview.layoutIfNeeded()
var contentInsets = UIEdgeInsets()
contentInsets = UIEdgeInsetsZero
scrollview.contentInset=contentInsets
scrollview.scrollIndicatorInsets = contentInsets
scrollview.contentSize = self.view.bounds.size
【讨论】:
它给出了同样的错误Auto Layout still required after sending -viewDidLayoutSubviews to the view controller.
以上是关于使用 UIScrollView 在 UIView layoutSublayersOfLayer 中断言失败的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UIScrollView 上转到 UIView 的顶部