[ad_1]
In a number of views I’ve an extension that implements the protocol UICollectionViewDelegateFlowLayout like this:
extension MyView: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: CGFloat((collectionView.body.measurement.width / 2) - 5), peak: CGFloat(165))
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
return 10
}
}
It really works nice, however the factor is I’ve a number of views with this identical actual code, besides that the extension is on every respective view. I assumed perhaps there’s a approach to scale back the quantity of code utilizing a protocol extension that offers all entry to those capabilities. I attempted one thing like this:
protocol A the place Self: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat
}
extension A {
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: CGFloat((collectionView.body.measurement.width / 2) - 5), peak: CGFloat(165))
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt part: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
return 10
}
}
However this does not take, and I believe it has one thing to do with the capabilities being delegate capabilities that I do not name by myself.
Anyway, is there a pleasant approach to take care of this?
[ad_2]
