Hi,
I further edited your code, I have added iPad, iPhone & unspecified categories using UserInterfaceIdium...
Code is totally working on iPhone but not working on iPad... It creates 4 UICollectionViews in Portrait & 5 in Landscape... Even I have specified 4 on both orientations...
I am testing this on iOS Simulator...
My Code:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var sizeArea = CGSize()
if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
print("It's iPad")
if self.view.frame.size.width < self.view.frame.size.height {
print("It's Portrait iPad")
/
let spacing = self.view.frame.size.width - 25
let itemWidth = spacing / 4
let itemHeight = itemWidth
sizeArea = CGSize(width: itemWidth, height: itemHeight)
} else {
print("It's Landscape iPad")
/
let spacing = self.view.frame.size.width - 25
let itemWidth = spacing / 3
let itemHeight = itemWidth
sizeArea = CGSize(width: itemWidth, height: itemHeight)
}
} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.phone) {
print("It's iPhone")
if self.view.frame.size.width < self.view.frame.size.height {
print("It's Portrait iPhone")
/
let spacing = self.view.frame.size.width - 15
let itemWidth = spacing / 2
let itemHeight = itemWidth
sizeArea = CGSize(width: itemWidth, height: itemHeight)
} else {
print("It's Portrait iPhone")
/
let spacing = self.view.frame.size.width - 20
let itemWidth = spacing / 3
let itemHeight = itemWidth
sizeArea = CGSize(width: itemWidth, height: itemHeight)
}
} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.tv) {
} else if (UIScreen.main.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiom.unspecified) {
print("It's Unspecified Device")
if self.view.frame.size.width < self.view.frame.size.height {
print("It's Portrait iPhone")
/
let spacing = self.view.frame.size.width - 15
let itemWidth = spacing / 2
let itemHeight = itemWidth
sizeArea = CGSize(width: itemWidth, height: itemHeight)
} else {
print("It's Portrait iPhone")
/
let spacing = self.view.frame.size.width - 20
let itemWidth = spacing / 3
let itemHeight = itemWidth
sizeArea = CGSize(width: itemWidth, height: itemHeight)
}
}
return sizeArea
}