【问题标题】:Assertion failure in UIView layoutSublayersOfLayer using UIScrollView使用 UIScrollView 在 UIView layoutSublayersOfLayer 中断言失败
【发布时间】: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)

    }

任何人都解决了这个问题,我不知道如何处理这个错误!

【问题讨论】:

    标签: ios swift uiview uiscrollview constraints


    【解决方案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.
    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    相关资源
    最近更新 更多