collectionViewContentSize in iOS 10 using self-sizing cells

China☆狼群 提交于 2019-12-03 13:15:35
orxelm

This is some strange bug in iOS10 with iPhone Plus devices only. I've faced the same issue, my solution was to call layoutIfNeeded like this:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    collectionView.layoutIfNeeded() // Patch: only to solve UIKit crash on Plus models
    return 1
}

Doing the same thing in different UICollectionViewDataSources methods will work as well

I got same issue when develop my app on iOS 10. Remove the first line solved my problem.

self.collectionView.frame = CGRectMake(0, 0, targetSize.width, FLT_MAX); //Remove this

Hope this help!

In my case, calling invalidateLayout before layout is a workaround for this issue.

In a UIViewController subclass:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    collectionView.collectionViewLayout.invalidateLayout()
}

or in a UIView subclass:

override func layoutSubviews() {
    super.layoutSubviews()
    collectionView.collectionViewLayout.invalidateLayout()
}

I got same issue when develop my app on iOS 10 and set UICollectionViewDataSource to itself on awakeFromNib,like this:

override func awakeFromNib() {
    super.awakeFromNib()
    let layout = UICollectionViewFlowLayout()
    // set your layout 
    collectionViewLayout = layout
    // set dataSource equal to self in here cause a crash
    dataSource = self
}

then I move the UICollectionViewDataSource setting code to layoutSubviews,the problem solved,like this:

override func layoutSubviews() {
        super.layoutSubviews()
        dataSource = self
    }

override func awakeFromNib() {
    super.awakeFromNib()
    let layout = UICollectionViewFlowLayout()
    // set your layout 
    collectionViewLayout = layout
    // set dataSource equal to self in here cause a crash
    dataSource = self
}

Hope this help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!