设置 layer.borderColor 时 UICollectionViewCell 边框消失
UICollectionViewCell border disappears when layer.borderColor is set
我正在尝试向 @IBDesignable UICollectionViewCell
原型添加细的彩色边框。在 Storyboard 中,我设置了 layer.cornerRadius
、layer.masksToBounds
和 layer.borderWidth
用户定义属性,边框按预期显示。
但是,如果我还设置自定义 layer.borderColor
,整个边框、半径和遮罩将从故事板中消失。在模拟器中,角半径(和掩码,大概)出现,但边界没有。
我也尝试过以编程方式设置它们,但正如 this year-old, unanswered Whosebug question 所示,这也不起作用。
根据要求,这是单元格的代码:
@IBDesignable open class ReleaseSpineCell: UICollectionViewCell {
public var masterRelease: MasterRelease? {
didSet {
artistName = masterRelease?.artistName
title = masterRelease?.title
catalogNumber = "none"
}
}
@IBInspectable public var artistName: String? {
didSet {
artistLabel?.text = artistName
}
}
@IBInspectable public var title: String? {
didSet {
titleLabel?.text = title
}
}
@IBInspectable public var catalogNumber: String? {
didSet {
catalogLabel?.text = catalogNumber
}
}
@IBOutlet private weak var artistLabel: UILabel?
@IBOutlet private weak var titleLabel: UILabel?
@IBOutlet private weak var catalogLabel: UILabel?
}
以及 UICollectionViewController
的 dataSource
的相关部分:
class CollectionModel: FetchedResultsCollectionModel {
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spineCell", for: indexPath) as! ReleaseSpineCell
cell.masterRelease = fetchedResultsController?.fetchedObjects?[indexPath.row] as? MasterRelease
return cell
}
}
FetchedResultsCollectionModel
是由 NSFetchedResultsController
支持的自定义集合视图数据源和委托,MasterRelease
是托管对象。我尽量让这些东西尽可能简单。
您面临的问题是您正在使用 Boolean
值 layer.borderColor
,您必须改用 Color
。但无论如何这不起作用,因为对于 layer.borderColor
你需要一个 cgColor
所以我认为你需要为 borderColor 定义一个 @IBInspectable var 并在代码中执行此操作
@IBDesignable open class ReleaseSpineCell: UICollectionViewCell {
public var masterRelease: MasterRelease? {
didSet {
artistName = masterRelease?.artistName
title = masterRelease?.title
catalogNumber = "none"
}
}
@IBInspectable public var artistName: String? {
didSet {
artistLabel?.text = artistName
}
}
@IBInspectable public var title: String? {
didSet {
titleLabel?.text = title
}
}
@IBInspectable public var catalogNumber: String? {
didSet {
catalogLabel?.text = catalogNumber
}
}
//added this inspectable property
@IBInspectable public var borderColor: UIColor? {
didSet {
self.layer.borderColor = borderColor!.cgColor
}
}
@IBOutlet private weak var artistLabel: UILabel?
@IBOutlet private weak var titleLabel: UILabel?
@IBOutlet private weak var catalogLabel: UILabel?
}
然后你就可以在情节提要中做到这一点
希望对您有所帮助
我正在尝试向 @IBDesignable UICollectionViewCell
原型添加细的彩色边框。在 Storyboard 中,我设置了 layer.cornerRadius
、layer.masksToBounds
和 layer.borderWidth
用户定义属性,边框按预期显示。
但是,如果我还设置自定义 layer.borderColor
,整个边框、半径和遮罩将从故事板中消失。在模拟器中,角半径(和掩码,大概)出现,但边界没有。
我也尝试过以编程方式设置它们,但正如 this year-old, unanswered Whosebug question 所示,这也不起作用。
根据要求,这是单元格的代码:
@IBDesignable open class ReleaseSpineCell: UICollectionViewCell {
public var masterRelease: MasterRelease? {
didSet {
artistName = masterRelease?.artistName
title = masterRelease?.title
catalogNumber = "none"
}
}
@IBInspectable public var artistName: String? {
didSet {
artistLabel?.text = artistName
}
}
@IBInspectable public var title: String? {
didSet {
titleLabel?.text = title
}
}
@IBInspectable public var catalogNumber: String? {
didSet {
catalogLabel?.text = catalogNumber
}
}
@IBOutlet private weak var artistLabel: UILabel?
@IBOutlet private weak var titleLabel: UILabel?
@IBOutlet private weak var catalogLabel: UILabel?
}
以及 UICollectionViewController
的 dataSource
的相关部分:
class CollectionModel: FetchedResultsCollectionModel {
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "spineCell", for: indexPath) as! ReleaseSpineCell
cell.masterRelease = fetchedResultsController?.fetchedObjects?[indexPath.row] as? MasterRelease
return cell
}
}
FetchedResultsCollectionModel
是由 NSFetchedResultsController
支持的自定义集合视图数据源和委托,MasterRelease
是托管对象。我尽量让这些东西尽可能简单。
您面临的问题是您正在使用 Boolean
值 layer.borderColor
,您必须改用 Color
。但无论如何这不起作用,因为对于 layer.borderColor
你需要一个 cgColor
所以我认为你需要为 borderColor 定义一个 @IBInspectable var 并在代码中执行此操作
@IBDesignable open class ReleaseSpineCell: UICollectionViewCell {
public var masterRelease: MasterRelease? {
didSet {
artistName = masterRelease?.artistName
title = masterRelease?.title
catalogNumber = "none"
}
}
@IBInspectable public var artistName: String? {
didSet {
artistLabel?.text = artistName
}
}
@IBInspectable public var title: String? {
didSet {
titleLabel?.text = title
}
}
@IBInspectable public var catalogNumber: String? {
didSet {
catalogLabel?.text = catalogNumber
}
}
//added this inspectable property
@IBInspectable public var borderColor: UIColor? {
didSet {
self.layer.borderColor = borderColor!.cgColor
}
}
@IBOutlet private weak var artistLabel: UILabel?
@IBOutlet private weak var titleLabel: UILabel?
@IBOutlet private weak var catalogLabel: UILabel?
}
然后你就可以在情节提要中做到这一点
希望对您有所帮助