我创建了一个继承自UICollectionViewFlowLayout的Swift类,并且我已经能够成功地完成一些巧妙的工作,比如更改它绘制的每个框之间的间距。我正在画一个日历,所以想象一下28-31个盒子,每行7个,取决于月份。
我从来没有想过如何做一些事情,比如将正确的边框设为空白,而不是..one。或者顶端边框。我最近将每个框之间的间距更改为0,现在框的边界重叠。
我喜欢以编程方式将所有右边框设置为空白,这样它的下一个边框的左边框就可以作为共同边框(所以它不是双边框)。然后,在集合中每一行的最后一个框上,我可以使其具有右边框。知道我在说什么吗?漂亮,干净,每个盒子周围的边框很薄。
我可以在我的override func layoutAttributesForElementsInRect(rect: CGRect) -> UICollectionViewLayoutAttributes中这样做吗?也许在别的地方?
谢谢!
发布于 2015-11-09 07:58:28
太棒了!我想通了。这很酷,因为现在我可以通过编程来决定何时添加下面的边框(例如,如果x,则day.layer.addSublayer(topBorder),否则不添加它)
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
let day = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CalendarDayCollectionViewCell
/* adds a border to entire grid */
day.layer.borderColor = UIColor.lightGrayColor().CGColor
//day.layer.borderWidth = 1 //can also do .cornerRadius to round out borders
//top border
let topBorder = CALayer()
topBorder.frame = CGRectMake(0.0, 0.0, day.frame.size.width, 1.0);
topBorder.backgroundColor = UIColor(white: 0.8, alpha: 1.0).CGColor
// bottom border
let bottomBorder = CALayer()
bottomBorder.frame = CGRectMake(0.0, day.frame.size.height-1, day.frame.size.width, 1.0);
bottomBorder.backgroundColor = UIColor(white: 0.8, alpha: 1.0).CGColor
let leftBorder = CALayer()
leftBorder.frame = CGRectMake(0.0, 0.0, 1.0, day.frame.size.height);
leftBorder.backgroundColor = UIColor(white: 0.8, alpha: 1.0).CGColor
let rightBorder = CALayer()
rightBorder.frame = CGRectMake(day.frame.size.width - 1, 0.0, 1.0, day.frame.size.height);
rightBorder.backgroundColor = UIColor(white: 0.8, alpha: 1.0).CGColor
day.layer.addSublayer(topBorder)
day.layer.addSublayer(bottomBorder)
day.layer.addSublayer(leftBorder)
day.layer.addSublayer(rightBorder)
return day
}发布于 2015-11-09 07:41:14
您不能真正显式删除边框的一侧或两侧,这不受CALayer的支持。
但是基于这个answer,您应该能够通过重叠单元格来实现,所以您应该覆盖func layoutAttributesForElementsInRect(rect: CGRect)是正确的
顺便说一句,我想你可以试试
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let borderWidth = 5
return UIEdgeInsetsMake(-borderWidth,-borderWidth,-borderWidth,-borderWidth);
}https://stackoverflow.com/questions/33599296
复制相似问题