UICollectionView Set number of columns
CollectionViews are very powerful, and they come at a price. Lots, and lots of options. As omz said:
there are multiple ways you could change the number of columns
I'd suggest implementing the <UICollectionViewDelegateFlowLayout>
Protocol, giving you access to the following methods in which you can have greater control over the layout of your UICollectionView
, without the need for subclassing it:
collectionView:layout:insetForSectionAtIndex:
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
collectionView:layout:referenceSizeForFooterInSection:
collectionView:layout:referenceSizeForHeaderInSection:
collectionView:layout:sizeForItemAtIndexPath:
Also, implementing the following method will force your UICollectionView to update it's layout on an orientation change: (say you wanted to re-size the cells for landscape and make them stretch)
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration{
[self.myCollectionView.collectionViewLayout invalidateLayout];
}
Additionally, here are 2 really good tutorials on UICollectionViews
:
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
http://skeuo.com/uicollectionview-custom-layout-tutorial
Specify row and column number for UICollectionView
Try this:
// 1
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 2;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 100;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.lbl.text = @"Data";
return cell;
}
I want the Collection view to show 2 Columns per Row, how do I do that? [with Swift 5 answers please]
You need not check the device size because we can use the collectionView width to calculate the width of the cell. Using the cell width you can calculate the height as per your need.
One more thing: You need to use UICollectionViewDelegateFlowLayout & confirm the delegate & implement method below
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let padding: CGFloat = 25
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: 115)
}
Related Topics
Error: Initializer for Conditional Binding Must Have Optional Type, Not 'String'
How to Change Wkwebview or Uiwebview Default Font
Using Nsuserdefaults with Xcode 8 and iOS 10
How to Add Images for Different Screen Size from Assets.Xcassets in Xcode 8
How to Rotate Orientation Programmatically in Swift
Trying to Set Only Time in Uidatepicker in Swift 2.0
Cannot Assign a Value of Type "String" to Type "Uilabel" in Swift
Swift: Can't Get Nsdate Dateformatter Right
Realmswift: Convert Results to Swift Array
iOS UI Testing on an Isolated View
What Are the Device-Width CSS Viewport Sizes of the Iphone6 and iPhone 6 Plus
Uitableview Checkmark Only One Row at a Time
Launch Apple Mail App from Within My Own App
Can't Assign Multiple Buttons to Uinavigationitem When Using Storyboard with iOS 5
Change the Width of Master in Uisplitviewcontroller