UICollectionViewFlowLayout minimumLineSpacing不适用于整个集合视图 – Swift

我正在为日历创buildUIView子类,其中带有stream布局的UICollectionView用于显示date。 代码是:

override func draw(_ rect: CGRect) { super.draw(rect) . . configureCalendarView() . } func configureCalendarView() { cellWd = Double(self.frame.size.width)/7.0 let layout = UICollectionViewFlowLayout.init() layout.minimumLineSpacing = 0.5 layout.minimumInteritemSpacing = 0.0 layout.sectionInset = UIEdgeInsetsMake(1, 0, 0, 0) collectionV = UICollectionView.init(frame: CGRect(x: 0, y:90, width:self.frame.size.width, height:CGFloat(cellWd * 5 + 3.5)), collectionViewLayout: layout) collectionV?.backgroundColor = self.backgroundColor collectionV?.isScrollEnabled = false collectionV?.register(DayCell.classForCoder(), forCellWithReuseIdentifier:reuseID) collectionV?.dataSource = self; collectionV?.delegate = self; collectionV?.backgroundColor = calBgColor //lightgray self.addSubview(collectionV!) } 

一切工作正常,但第二行和第三行之间的最小行间距没有任何影响。 以下是截图:

在这里输入图像说明

怎么了? 任何帮助将不胜感激。 谢谢 !

layout.minimumLineSpacing = 0.5转换为0.5point * scale =像素。

所以,如果你使用的是3.0级以上的iphone 6+ 7+,那么0.5就是1.5像素。 而在2倍的比例将是1个像素。 纠正这一点。 添加这个:

layout.minimumLineSpacing = 1.0 / [UIScreen mainScreen] .scale; 这样你保证总是得到1像素的间距。

编辑:

 layout.minimumLineSpacing = 1.0f; CGFloat collectionWidth = self.frame.size.width; CGFloat days = 7.0f; CGFloat spacing = 1.0f; CGFloat itemHeight = floorf((collectionWidth/days) - spacing;