I am trying to do something like this:
Basically, I am using a UICollectionView
and the cells (3 diferent .xib
).
So far, it works.
The thing I want to do is:
- Set a
autoheight
- If rotate, add 1 row to the
UIColectionView
2.1 If tablet, on portrait will have 2 rows and landscape 3 rows. (which basically is the same of point 2, only adding 1 row.
I have something like this:
extension ViewController {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
setSizeSize()
}
func setSizeSize(){
if(DeviceType.IS_IPAD || UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight){
if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = CGSize(width: 1, height: 1)
layout.invalidateLayout()
}
}else{
if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
layout.invalidateLayout()
}
}
myCollectionView.collectionViewLayout.invalidateLayout()
}
}
Does not work. Also, it freezes device. On simulator works parcially. (I'm trusting more device)
I also tried this, but it works sometimes...
Please, let me know if you need more info.
Thank you all in advance for the help.
This could be another alternative:
To use this:
1) In the storyboard select your collectionView ( Es. myCollectionView ) and search for CollectionViewLayout 2) Click on the class inspector and change the class to MyCollectionViewLayout.
3) In your viewDidLoad:
4) Add the two protocol stubs:
I can suggest you to create your own
UICollectionViewFlowLayout
subclass which would generate needed layout. Here is a simple flow layout that you can use. You can adjust it to your needs, if I missed something.Now in your UIViewController you can use it like this:
invalidate
collectionView
layout after orientation would be changedand now make your
ViewController
conform toCollectionViewFlowLayoutDelegate