Reputation: 9065
I have add a uicollectionView to the super view and decide the cell size like this:
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
if(!cell){
cell = [[UICollectionViewCell alloc]init];
}
UIImageView *imageView = [[UIImageView alloc]initWithFrame:cell.bounds];
int r = arc4random_uniform(4);
NSString *imageName = [[NSString stringWithFormat:@"%d", r] stringByAppendingString:@".jpg"];
imageView.image = [UIImage imageNamed:imageName];
[cell addSubview:imageView];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(180, 340);
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1500;
}
And it looks like this on the device:
I want the space between two images and the padding and tailing of each row to be equal.
I also want to set the space between each row.
How can I do that?
Upvotes: 0
Views: 1787
Reputation: 411
try this
pass the CGSizeMake what ever you want
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake((self.view.frame.size.width/5) , (self.view.frame.size.width/5) - 25);
}
Upvotes: 0
Reputation: 9503
From the storyboard, select UICollectionView
, goto property and change Inset.
Edit Do it by code
Add UICollectionViewDelegateFlowLayout
class SecondVC: UIViewController , UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout
Implement this below func
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}
Output
In Obj-c
Use this func
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;
and return UIEdgeInsetsMake(10, 10, 10, 10);
Upvotes: 1
Reputation: 5831
func UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsets
Description
Creates an edge inset for a button or view.
An inset is a margin around a rectangle. Positive values represent margins closer to the center of the rectangle, while negative values represent margins further from the center.
sectionInset will give space on Leading and Trailing of collectionview scetion
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(0, 4.0, 0, 4.0)
self.collectionView = UICollectionView(frame: coll_frame, collectionViewLayout: layout)
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(PreferenceCollectionViewCell.self, forCellWithReuseIdentifier: String(describing: PreferenceCollectionViewCell.self))
self.addSubview(self.collectionView)
These two methods will give space to each cell, first you can pass 0.0 and try, how it works.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 6
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 6
}
Upvotes: 3
Reputation: 69
Provide inter cell spacing and inter line spacing for cell. It adjusts the cell based on the value provided.
Hope this helps.
Upvotes: -1