问题
I have an issue with systemLayoutSizeFittingSize on iOS 13 beta. I am working on a plugin for NativeScript which uses the systemLayoutSizeFittingSize of the UICollectionViewCell to "measure and layout" the UIView that is in the cell. What I have been doing is:
- Manually call
systemLayoutSizeFittingSizeif I need to force a measure/layout of theUIViewthat is in the cell
or
- Wait for the OS to call
systemLayoutSizeFittingSizeand again measure and layout theUIViewthat is in the cell
It all worked perfectly until iOS 13 beta where it looks like there has been a change in the way the systemLayoutSizeFittingSize API is being called by the OS itself. In iOS 13 beta that API is not called by the OS at all and is rather using the collectionView:layout:sizeForItemAtIndexPath: API to determine the size of a UICollectionViewCell. In iOS 12 and lower after the UICollectionView finishes its initial layout pass it calls each UICollectionViewCell's systemLayoutSizeFittingSize which allows you to give a chance for the cell to tells its size. In iOS 13 systemLayoutSizeFittingSize is no longer called.
Maybe it is something that I have to change or call on the UIView itself or the UICollectionView to make it work as it used to in iOS 12 but I am a bit lost in understanding if I am using the systemLayoutSizeFittingSize correctly.
So my question is, Should I expect the systemLayoutSizeFittingSize to be called by the OS automatically or this is simply a way to manually force a UICollectionViewCell to tell its size? And overall am I using it correctly?
Edit:
After further investigation, it looks like there is a change in the systemLayoutSizeFittingSize on iOS 13 (beta). It is no longer being called as it used to be when the estimatedItemSize of the layout is set. I created a project that shows the issue caused by this change in behavior and I will be reporting this to the iOS team. The project can be found here. This issue leads to the behavior where you cannot implement "Self-sizing" cells when working with UICollectionView as the systemLayoutSizeFittingSize is no longer being called.
回答1:
It looks like there has been an internal breaking change with current iOS 13.0 beta (simulator Version 11.0 (SimulatorApp-895.6 SimulatorKit-553.12 CoreSimulator-643.11) that is not documented in the release notes. The change is that systemLayoutSizeFittingSize of an UICollectionViewCell is no longer called while the systemLayoutSizeFitting(_:withHorizontalFittingPriority:verticalFittingPriority:) is called.
So:
systemLayoutSizeFittingSizeis called on iOS <12 whilesystemLayoutSizeFitting(_:withHorizontalFittingPriority:verticalFittingPriority:)is called on iOS >13
Solution/Workaround
So as a solution/workaround you have to duplicate your code from systemLayoutSizeFittingSize and place it inside systemLayoutSizeFitting(_:withHorizontalFittingPriority:verticalFittingPriority:). While this is not perfect solution/workaround it at least gives us the a working solution for now, it is very much possible that this can change before iOS 13 is out of beta version and is released and is no longer required. Code example:
import UIKit
class FlickPhotoCell: UICollectionViewCell {
func getCellSize(_ targetSize: CGSize) -> CGSize {
return CGSize(width: 50, height: 200)
}
// Only this is called on iOS 12 and lower
override func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize {
return self.getCellSize(targetSize)
}
// Only this is called on iOS 13 beta
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
return self.getCellSize(targetSize)
}
}
来源:https://stackoverflow.com/questions/57184502/systemlayoutsizefittingsize-not-called-on-ios-13