I'd like someone could explain clearly how I should calculate the math for the UICollectionViewFlowLayout, exactly "the subtract spacing" thing from itemSize width/height. in this example I get a squared view, 3 items in 3 columns.
- why if I use
width: (view.frame.width/3)-testSpacing-1.5
the grid gives standard two columns and empty at the center? and if is use:
width: (view.frame.width/3)-testSpacing-6
nothing seems to change?
Am I doing it right? but why someone told me I should subtract half of the spacing and not the whole value?
Is it normal that if I rotate device the space between cells becomes greater? is there a "pattern" to manage that thing?
var collectionData = ["1", "2", "3 ", "4", "5", "6", "7", "8", "9", "10", "11", "12","1", "2", "3 ", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
let testSpacing: CGFloat = 2
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(
width: (view.frame.width/3)-testSpacing-5,
height: (view.frame.width/3)-testSpacing-5)
layout.minimumInteritemSpacing = 5
layout.minimumLineSpacing = 5
layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
myCollectionView.collectionViewLayout = layout
my cell:
class Test_CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var messageLabel: UILabel!
static let identifier = "Test_CollectionViewCell"
private var message: String = "" {
didSet {
self.messageLabel.text = message
}
}
func setup(message: String) {
self.backgroundColor = .systemTeal
self.messageLabel.backgroundColor = .white
if message == "1" {
self.messageLabel.backgroundColor = .red
}
self.message = message
// contentView.clipsToBounds = true
}
override func prepareForReuse() {
self.backgroundColor = .white
self.messageLabel.backgroundColor = .white
}
}



