Reputation: 451
I want my cell's width to be the same as the screen's width on every screen size. I can't set constraints in the storyboard since the option is greyed out, and setting them programmatically results in an error that says something like "Unable to activate constraint with anchors...because they have no common ancestor" even though I'm only setting the constraints to be the view's leading and trailing anchors.
The way the storyboard is setup now, the cell is at the desired width on iphone SE and that same width on the larger screen sizes.
Upvotes: 3
Views: 3081
Reputation: 204
you can use delegate method
viewcontroller:UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize.init(
width: collectionView.bounds.height,
height: collectionView.bounds.height
)
}
Upvotes: 3
Reputation: 1
This might help you to set the collectionView
width same as the screen width.
In your viewDidLoad()
write this code
let height: CGFloat = 100.0 //Some constant
let width: CGFloat = self.view.frame.width
let spLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
spLayout.itemSize = CGSize(width: width, height: height)
Upvotes: 0