Apologies if the title of my question is unclear but essentially I want to make a UICollectionView like the Medium app below:
I have made a UICollectionView and this is what it looks like so far:
I want to decrease the spacing between each cell (red lines) so that they are closer together and there is space between the sides of the collectionview and the cells that are on the border. I have used minimumInteritemSpacing
and minimumLineSpacing
but they have no effect on the red space at all.
Here is my code:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let data = ["Autos", "Cleaning", "Technology", "Business", "Sports", "Childcare", "Airsoft", "Cycling", "Fitness", "Baseball", "Basketball", "Bird Watching", "Bodybuilding", "Camping", "Dowsing", "Driving", "Fishing", "Flying", "Flying Disc", "Foraging", "Freestyle Football", "Gardeing", "Geocaching", "Ghost hunting", "Grafitti", "Handball", "High-power rocketry", "Hooping", "Horseback riding", "Hunting"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let layout = UICollectionViewFlowLayout.init()
layout.scrollDirection = .vertical
layout.minimumInteritemSpacing = 5.0
layout.minimumLineSpacing = 20.0
let collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(collectionViewCell.self, forCellWithReuseIdentifier: "cell")
collectionView.backgroundColor = .white
self.view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: self.view.layoutMarginsGuide.topAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 18.0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -18.0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: self.view.layoutMarginsGuide.bottomAnchor).isActive = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! collectionViewCell
cell.textLabel.text = data[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let messageText = data[indexPath.row]
let size = CGSize.init(width: collectionView.frame.size.width, height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let estimatedFrame = NSString.init(string: messageText).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 15.0, weight: .regular)], context: nil)
return CGSize.init(width: estimatedFrame.width + 20.0, height: estimatedFrame.height + 20.0)
}
}
class collectionViewCell: UICollectionViewCell {
var textLabel: UILabel = {
let label = UILabel.init()
label.font = UIFont.systemFont(ofSize: 15.0, weight: .regular)
label.textColor = UIColor.black
label.textAlignment = .center
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.contentView.addSubview(textLabel)
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
textLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
setupShadow()
}
func setupShadow() {
self.contentView.backgroundColor = .white
self.contentView.layer.cornerRadius = 2.0
self.contentView.clipsToBounds = true
let shadowSize : CGFloat = 1.0
let shadowPath = UIBezierPath(rect: CGRect(x: -shadowSize / 2,
y: -shadowSize / 2,
width: self.contentView.frame.size.width + shadowSize,
height: self.contentView.frame.size.height + shadowSize))
self.contentView.layer.masksToBounds = false
self.contentView.layer.shadowColor = UIColor.black.cgColor
self.contentView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.contentView.layer.shadowOpacity = 0.5
self.contentView.layer.shadowPath = shadowPath.cgPath
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}