Reputation: 453
Please let me know How to make grid view like 2 cell per each column for portrait and 3 cell for landscape. Using collection view custom cell without using pods or third party framework.
Thanks in advance.
Upvotes: 1
Views: 1288
Reputation: 706
I have answered similar question like this here. I have even explained how to change the view to match your requirement. In your case it might look something like this.
let orientation = UIApplication.sharedApplication().statusBarOrientation
if(orientation == .LandscapeLeft || orientation == .LandscapeRight)
{
return CGSizeMake((yourCollectionView.frame.size.width-10)/3, (yourCollectionView.frame.size.height-10)/3)
}
else{
return CGSizeMake((yourCollectionView.frame.size.width-5)/2, (yourCollectionView.frame.size.height-5)/2)
}
The deduction is the cell padding(which is 5 in my case). If 3 cells, then 2 cell padding making total of 10 deduction and if 2 cells, then 1 cell padding making total of 5 deduction. Hope this helps.
Upvotes: 1
Reputation: 3875
you need to do like this
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let collectionViewSize = collectionView.frame.size.width
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
return CGSizeMake(collectionViewSize/3, collectionViewSize/3)
}
else if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
return CGSizeMake(collectionViewSize/2, collectionViewSize/2)
}
return CGSizeZero
}
you also need to reload on orientation change
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
func rotated()
{
collectionView.reloadData()
}
Note: Make sure you will confirm UICollectionViewDelegateFlowLayout
protocol
Upvotes: 1