ios - 使用 UIScrollView 在 UIView layoutSublayersOfLayer 中断言失败

标签 ios swift uiview uiscrollview constraints

我制作了一个使用 UIScrollView 在 Swift 中的 UIViewController 之间滚动的应用程序,它在 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)

    }

任何人都可以解决此问题,我不知道如何处理此错误!

最佳答案

viewDidLayoutSubviews 被多次调用

在 viewDidLayoutSubviews() 中尝试此代码

 scrollview.layoutIfNeeded()
 var contentInsets = UIEdgeInsets()
 contentInsets = UIEdgeInsetsZero
 scrollview.contentInset=contentInsets
 scrollview.scrollIndicatorInsets = contentInsets
 scrollview.contentSize = self.view.bounds.size

关于ios - 使用 UIScrollView 在 UIView layoutSublayersOfLayer 中断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731657/

相关文章:

ios - 如何在 iOS 上找到最顶层的 View Controller

swift - 以编程方式使用约束的自定义 UIView

ios - GoogleMaps Swift 点击标记

ios - UI分段控制ui修改

swift - 如何快速将一个应用程序的文本字段数据发送到另一个应用程序

ios - 在 Swift 中将参数传递给选择器

iOS Swift - 使用可以取消的临时 UIView

ios - 如何自定义 UITextField 的数字输入?

ios - 将 UIProgressView 进度设置为 0

objective-c - 如何使用 setContentView 更改停靠栏图标以在 mac os x 中显示一个大字符