Reputation: 4352
I have UICollectionViewCell
s that have only one UIImageView
. I am using RxSwift
for the project. The delegate
is set in the Main.storyboard
, and the datasource
is set by the RxSwift
. I need to remove the spaces between the cells, but having trouble with that. The methods from this thread are not working:
Aligning collection view cells to fit exactly 3 per row
I tried both answers but nothing happens.
Here is the UIViewCollectionController
s' code:
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
//self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
collectionView?.dataSource = nil
setupCellConfiguration()
}
private func setupCellConfiguration() {
//1
immuneCells
.bind(to: self.collectionView!
.rx //2
.items(cellIdentifier: CellTypeCollectionViewCell.Identifier,
cellType: CellTypeCollectionViewCell.self)) { // 3
row, cellType, cell in
cell.configureWithCellType(cellType: cellType) //4
}
.disposed(by: disposeBag) //5
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
let num = CellType.immune.count
return num
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenWidth = UIScreen.main.bounds.width
let scaleFactor = (screenWidth / 3) - 6
return CGSize(width: scaleFactor, height: scaleFactor)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets.zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
Any suggestions would be greatly appreciated
Update
I tried setting up equal width and height, top, left constraints of UIImageView
but it did not help.
This is how it looks:
Upvotes: 4
Views: 5641
Reputation: 859
Try
flowLayout.minimumLineSpacing = 0
flowLayout.minimumInteritemSpacing = 0
Upvotes: 3
Reputation: 1052
You should use UICollectionViewFlowLayout
.
Below is sample code:
var flowLayout = UICollectionViewFlowLayout()
let numberOfItemsPerRow = 3
let itemSpacing = 5.0
let lineSpacing = 5.0
let width = ((self.collectionView?.width)! - itemSpacing*(numberOfItemsPerRow+1)) / numberOfItemsPerRow
flowLayout.itemSize = CGSize(width: width, height: width)
flowLayout.minimumInteritemSpacing = itemSpacing
flowLayout.sectionInset = UIEdgeInsetsMake(lineSpacing, lineSpacing, lineSpacing, lineSpacing)
self.collectionView?.collectionViewLayout = flowLayout
Upvotes: 1
Reputation: 154583
From the comments, it is apparent that your FlowLayout
methods are not being called.
Add UICollectionViewDelegateFlowLayout
to your UICollectionView
's class
definition line.
Something like:
class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
That will tell iOS that your class
implements that protocol and supports the layout methods so that it knows to call them.
Upvotes: 2