[英]Strange Border on UICollectionViewCell - Swift
I have created a subclass for a UICollectionViewCell, which looks like this... 我为UICollectionViewCell创建了一个子类,看起来像这样...
import Foundation
import UIKit
class GroupCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var subject: UILabel!
}
As soon as I override the drawRect function like this... 一旦我像这样重写drawRect函数...
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 4
}
I get a strange border/shadow on the top and right of each cell (background colour set in storyboard)... 我在每个单元格的顶部和右侧都有一个奇怪的边框/阴影(在情节提要中设置了背景颜色)...
Note that even if I take out the self.layer.cornerRadius line, the borders still appear, so presumably I've missed something out at the drawRect function - I'm just not sure what. 请注意,即使我取出self.layer.cornerRadius行,边框仍然会出现,所以大概我在drawRect函数中错过了一些东西-我只是不确定。
What makes this especially peculiar, is that when I run the code on a iPhone 6+ or wider device, the problem goes away. 之所以如此特别,是因为当我在iPhone 6+或更高版本的设备上运行代码时,问题就消失了。
The only other (presumably) relevant code, is that which I use to size the cells appropriately in the view controller: 唯一其他(大概)相关的代码是我用来在视图控制器中适当调整单元格大小的代码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = collectionView.frame.width
return CGSize(width: (size/3)-8, height: (size/3)-8)
}
What is the grey border/shadow, and how can I get rid of it? 什么是灰色边框/阴影,如何消除它? Many thanks in advance!
提前谢谢了!
I suspect this is probably just an implementation quirk that appears because you're not quite doing things the way Apple wants. 我怀疑这可能只是一个实现上的怪癖,因为您没有按照苹果想要的方式做事。 Unless you're subclassing
UIView
, you should call super.drawRect()
in your own drawRect()
method. 除非要
super.drawRect()
UIView
子类,否则应在自己的drawRect()
方法中调用super.drawRect()
。 Furthermore, drawRect()
isn't really the right place to set cornerRadius
: you should set that once, perhaps when the cell is created, then forget about it. 此外,
drawRect()
并不是设置cornerRadius
的正确位置:您应该设置一次(也许在创建单元格时),然后再忽略它。
You might find Apple's reference for UIView and drawRect useful: 您可能会发现Apple的UIView和drawRect参考非常有用:
If you subclass UIView directly, your implementation of this method does not need to call super.
如果直接子类化UIView,则此方法的实现无需调用super。 However, if you are subclassing a different view class, you should call super at some point in your implementation.
但是,如果要子类化其他视图类,则应在实现中的某个时刻调用super。
One more thing: if you can avoid overriding drawRect()
you should; 还有一件事:如果您可以避免覆盖
drawRect()
,则应该; it does have a performance impact. 它确实会对性能产生影响。
I can only suggest to make a little hack: Adding this lines to drawRect 我只能建议做一点修改:在drawRect中添加以下行
self.layer.borderWidth = 1
self.layer.borderColor = COLOR_OF_GRPUP_CELL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.