Reputation: 1699
i am using swift 2.o collection view. In that every thing is working fine. But when i run in different screen , the width gap between each cell are different. I need three cell in each row of collection view with 8 px gap for left,right,top,bottom for all cell in collection view. And also i need to set the cell as corner radius. i need like below image : [![enter image description here][1]][1]
But when i run in 5s screen i am getting like this and for 6 screen like 2nd image :
[![enter image description here][2]][2]
iphone 6 :
[![enter image description here][3]][3]
See the gap between left, right, bottom , top are not equally sapced as like my first image. i need to get like my first image.
I set the min line space for cell is 6 in storyboard. But i not able to get like my first imge.
please help me out.
my popvc.swift :
import UIKit
class popVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var tableData: [String] = ["RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI", "RESTAURANTS", "BANKS","COFFEE","PIZZA", "HOTELS", "TAXI","RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI"]
var tableImages: [String] = ["img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png", "img7.png", "img8.png", "img9.png", "img10.png", "img11.png", "img11.png", "img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tableData.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell
cell.lblCell.text = tableData[indexPath.row]
cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Cell \(indexPath.row) selected")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Upvotes: 1
Views: 9051
Reputation: 4180
From the storyboard set CollectionViews Section Inset from size inspector as Top = 8, Bottom = 8, Left = 8 and Right = 8, it should look like as
And return size for cell as
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//If you want your cell should be square in size the return the equal height and width, and make sure you deduct the Section Inset from it.
return CGSizeMake((self.view.frame.size.width/3) - 16, (self.view.frame.size.width/3) - 45);
}
Thats it, Nothing more. You should see the result as mine:
Upvotes: 6
Reputation: 283
@user5513630 Add following code in collection view.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = UIScreen.mainScreen().bounds.size
// 8 - space between 3 collection cells
// 4 - 4 times gap will appear between cell.
return CGSize(width: (size.width - 4 * 8)/3, height: 40)
}
Upvotes: 1
Reputation: 49730
Create a collection view using following code:
@IBOutlet var collectionView: UICollectionView?
var screenSize: CGRect!
var screenWidth: CGFloat!
var screenHeight: CGFloat!
override func viewDidLoad() {
print("select block is \(self.strBlockname)")
screenSize = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: (screenWidth/3)-1, height: (screenWidth/3)-1)
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1
collectionView = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight-65), collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView!.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView!)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.blackColor()
cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
cell.imageView?.image = UIImage(named: "circle")
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Its Output for iPhone5s,6s and 6+ is following:
iPhone5s
iPhone6s
Upvotes: 3