Collection View Flow layout For Both Orientation
In this session we will learn collection view flow layout for both orientation support
First add UICollectionViewDelegateFlowLayout in your View Controller
Step : 3
// Collection View Cell Layout
Step : 1
First add UICollectionViewDelegateFlowLayout in your View Controller
Step : 2 Add this function in your View Controller
@IBOutlet weak var collDashboard: UICollectionView!
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
DispatchQueue.main.async {
self.collDashboard?.collectionViewLayout.invalidateLayout()
self.collDashboard.layoutIfNeeded()
self.collDashboard.reloadData()
}
}
Step : 3
// we will change the section Insets

Step : 4
// Collection View Cell Layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
if UIDevice.current.orientation.isLandscape{
let dimension:CGFloat = self.view.frame.size.width / 5
return CGSize(width: dimension, height: collectionView.frame.size.height/2)
}
else if UIDevice.current.orientation.isFlat{
if self.view.frame.size.width > self.view.frame.size.height{ //Landscape
let dimension:CGFloat = self.view.frame.size.width / 5
return CGSize(width: dimension, height: collectionView.frame.size.height/2)
}
else{
let dimension:CGFloat = self.view.frame.size.width / 2
return CGSize(width: dimension, height: collectionView.frame.size.height/5)
}
}
else {
let dimension:CGFloat = self.view.frame.size.width / 2
return CGSize(width: dimension, height: collectionView.frame.size.height/5)
}
}
Comments
Post a Comment