滚动whist旋转时UICollectionView崩溃(索引路径上的补充项的布局属性发生了变化而没有失效..)

时间:2015-10-29 21:40:48

标签: ios uicollectionview uicollectionviewlayout

任何想法如何解决滚动whist旋转时我的UICollectionView崩溃的事实?

我使用以下方法分别滚动和旋转,每个似乎都工作正常。我刚刚注意到在做这两件事的同时我可以得到这个崩溃。因此,当我旋转设备并且在prepareLayout中计算新的布局属性时,似乎与事实有关,即连续滚动触发" invalidateLayoutWithContext(invalidContext)" (见下文)。

想法?是否有办法在旋转期间将任何滚动响应置于保持状态(或忽略它们)?

轮换方法 在视图控制器中的viewWillLayoutSubviews中,我使整个布局无效

self.cal.collectionViewLayout.invalidateLayout()

滚动方法 让我有一个"粘性"装饰视图(标题)我不会使整个布局无效,因为它会影响性能,但请执行以下操作。在布局类中,我重写了shouldInvalidateLayoutForBoundsChange

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        let invalidContext : UICollectionViewLayoutInvalidationContext = self.invalidationContextForBoundsChange(newBounds)

        // Keep Header Sticky
invalidContext.invalidateDecorationElementsOfKind(GCCalendarLayoutKind_Decorative1, atIndexPaths: [headerDecorativeIndexPath])

        // Apply Invalidation
        self.invalidateLayoutWithContext(invalidContext)        

        // Return normal super return (just in case of future IOS upgrades)
        return super.shouldInvalidateLayoutForBoundsChange(newBounds)
    }

请注意,我在装饰视图(标题)中失效,而崩溃的错误是关于我的补充视图布局不同。

错误

  

2015-10-30 07:14:30.181 test3_collectionview [17086:3102132] *   断言失败 - [UICollectionViewData validateLayoutInRect:],   /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UICollectionViewData.m:408   2015-10-30 07:14:30.185 test3_collectionview [17086:3102132] *   由于未捕获的异常而终止应用程序   ' NSInternalInconsistencyException',原因:'布局属性   索引路径上的补充项目({length =   2,path = 0 - 0})改为    指数   路径:({length = 2,path = 0 - 0});元件   种类:(装饰1); frame =(0 1085.5; 320 16); zIndex = 1;至    指数   路径:({length = 2,path = 0 - 0});元件   种类:(装饰1); frame =(0 853.5; 320 16); zIndex = 1;无   使布局无效'   ***首先抛出调用堆栈:

1 个答案:

答案 0 :(得分:1)

我不确定这是否足够但我会尝试沿着这些方向尝试(集合视图是滚动视图):

import CoreGraphics

class myController: UIViewController, UIScrollViewDelegate {

    var myScrollView = UIScrollView()

    override func viewDidLoad() {
        self.view.addSubview(myScrollView)
    }

    // This will capture rotation events
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        self.myScrollView.scrollEnabled = false
        coordinator.notifyWhenInteractionEndsUsingBlock( {_ in self.myScrollView.scrollEnabled = true} )
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        // This will not ignore scroll but perhaps can help keeping things "tidy" and "performant" during rotation.  Not sure
        if myScrollView.scrollEnabled == false {
            let offset = scrollView.contentOffset
            myScrollView.setContentOffset(offset, animated: false)
        }
    }   
}