In iOS 12, when does the UICollectionView layout cells, use autolayout in nib

后端 未结 7 1690
一整个雨季
一整个雨季 2020-11-28 03:08

Same code like this

collectionLayout.estimatedItemSize = CGSize(width: 50, height: 25)
collectionLayout.itemSize = UICollectionViewFlowLayoutAutomaticSize
co         


        
7条回答
  •  无人及你
    2020-11-28 03:32

    We had the same problem on our project. We also noticed differences between the multiple devices in iOS 12, requiring a call to layoutIfNeeded & invalidateLayout. The solution is based on @DHennessy13 's approach but doesn't require a boolean to manage states that seemed slightly hacky.

    Here it is based on an Rx code, basically the first line is when the data is changing, inside the subscribe is what needs to be done to fix the nasty iOS 12 UI glitch:

            viewModel.cellModels.asObservable()
                .subscribe(onNext: { [weak self] _ in
                    // iOS 12 bug in UICollectionView for cell size
                    self?.collectionView.layoutIfNeeded()
    
                    // required for iPhone 8 iOS 12 bug cell size
                    self?.collectionView.collectionViewLayout.invalidateLayout()
                })
                .disposed(by: rx.disposeBag)
    

    Edit:

    By the way, it seems to be a known issue in iOS 12: https://developer.apple.com/documentation/ios_release_notes/ios_12_release_notes (in UIKit section).

提交回复
热议问题