Reputation: 51
I'm setting up a collection view and I was having trouble defining the correct size of the cells for each app size, so I decided to do it manually through flowLayout.itemSize.width and flowLayout.itemSize.height, but to do it that way I need to check against each device, like the iPhone 6/7 and iPhone 6/7 Plus to correctly setup the cell sizes. So, how do I check against the specific devices to solve my problem?
Upvotes: 0
Views: 735
Reputation: 51
So, that is pretty much the way I solved the problem. I first got the correct size of the width and height for every device, then I did a check based on it. Inside each conditional, I finish setting up the design of my collectionView cells.
func adjustPetCellDesign() { //Get the flowLayout to set margins for the petCell guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return } flowLayout.minimumInteritemSpacing = margin flowLayout.minimumLineSpacing = margin flowLayout.sectionInset = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
//Get the screenSize to get width and height of every device.
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
//If iPhone is 6(s) or 7(s), set width and height as shown below
if screenWidth == 375 && screenHeight == 667 {
flowLayout.itemSize.height = 171.5
flowLayout.itemSize.width = 171.5
//If iPhone is 6(s)/7(s) Plus, set width and height as shown below
} else if screenWidth == 414 && screenHeight == 736 {
flowLayout.itemSize.height = 194
flowLayout.itemSize.width = 194
//If iPhone is 5(s)/SE, set width and height as shown below
} else if screenWidth == 320 && screenHeight == 568 {
flowLayout.itemSize.height = 147.5
flowLayout.itemSize.width = 147.5
}
}
Upvotes: 2
Reputation: 536057
Do not check against device types. Look at the screen size. You can readily do that through the UIScreen class.
Upvotes: 1