I am encountering a problem on landscape orientation for my collection view cell. When the app is in portrait it gives me the correct number of cell per row which is 2. But when
Swift 5
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var flag:CGSize? = nil
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad
{
if UIApplication.shared.statusBarOrientation.isPortrait{
let cellWidth = floor((collectionView.bounds.width - 5) / 2)
flag = CGSize(width: cellWidth, height: cellWidth)
}
else if UIApplication.shared.statusBarOrientation.isLandscape{
let cellwidth = floor((collectionView.bounds.width - 5) / 4)
flag = CGSize(width: cellwidth, height: cellwidth)
}
}
else if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone{
if UIApplication.shared.statusBarOrientation.isLandscape {
let cellWidth = floor((collectionView.bounds.width - 5) / 2)
flag = CGSize(width: cellWidth, height: cellWidth)
} else if UIApplication.shared.statusBarOrientation.isPortrait{
// let cellWidth = floor((collectionView.bounds.width - 5))
flag = CGSize(width: 402 , height: 185)
}
}
return flag!
}