gpt4 book ai didi

ios - UICollectionViewCell 上的奇怪边框 - Swift

转载 作者:行者123 更新时间:2023-11-28 12:59:29 24 4
gpt4 key购买 nike

我已经为 UICollectionViewCell 创建了一个子类,它看起来像这样......

import Foundation
import UIKit
class GroupCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var subject: UILabel!
}

只要我像这样覆盖 drawRect 函数...

override func drawRect(rect: CGRect) {
self.layer.cornerRadius = 4
}

我在每个单元格的顶部和右侧都有一个奇怪的边框/阴影( Storyboard中设置的背景颜色)...

Border on top and right of each cell

请注意,即使我删除了 self.layer.cornerRadius 线,边框仍然出现,所以大概我在 drawRect 函数中遗漏了一些东西 - 我只是不确定是什么。

特别奇怪的是,当我在 iPhone 6+ 或更宽的设备上运行代码时,问题就消失了。

唯一的其他(可能)相关代码是我用来在 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)
}

什么是灰色边框/阴影,我该如何摆脱它?非常感谢!

最佳答案

我怀疑这可能只是出现的一个实现怪癖,因为您没有按照 Apple 想要的方式做事。除非您是 UIView 的子类,否则您应该在您自己的 drawRect() 方法中调用 super.drawRect()。此外,drawRect() 并不是设置 cornerRadius 的正确位置:您应该设置一次,也许在创建单元格时设置,然后忘记它。

您可能会找到 Apple's reference for UIView and drawRect有用:

If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.

还有一件事:如果你可以避免覆盖 drawRect() 你应该;它确实会影响性能。

关于ios - UICollectionViewCell 上的奇怪边框 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34250297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com