【发布时间】:2018-07-19 20:11:38
【问题描述】:
抱歉,如果我的问题的标题不清楚,但本质上我想制作一个 UICollectionView,如下面的 Medium 应用程序:
我已经制作了一个 UICollectionView,到目前为止它是这样的:
我想减小每个单元格之间的间距(红线),以便它们更靠近在一起,并且在 collectionview 的两侧和边界上的单元格之间有空间。我使用了minimumInteritemSpacing 和minimumLineSpacing,但它们对红色空间完全没有影响。
这是我的代码:
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")
}
}
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout