我试图为@IBDesignable UICollectionViewCell
原型添加细的彩色边框。在故事板中,我设置了layer.cornerRadius
,layer.masksToBounds
和layer.borderWidth
用户定义的属性,边框按预期显示。
但是,如果我还设置了自定义layer.borderColor
,则整个边框,半径和蒙版会从故事板中消失。在模拟器中,出现角半径(和大概是掩模),但是边框不是。
我也尝试过以编程方式设置它们,但正如this year-old, unanswered StackOverflow 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
是托管对象。我尽量保持这些东西尽可能简单。
答案 0 :(得分:1)
您遇到的问题是您使用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?
}
然后您就可以在故事板中执行此操作
希望这有帮助