设置layer.borderColor时,UICollectionViewCell边框消失

时间:2017-08-06 22:41:08

标签: ios uicollectionview border uicollectionviewcell calayer

我试图为@IBDesignable UICollectionViewCell原型添加细的彩色边框。在故事板中,我设置了layer.cornerRadiuslayer.masksToBoundslayer.borderWidth用户定义的属性,边框按预期显示。

enter image description here

但是,如果我还设置了自定义layer.borderColor,则整个边框,半径和蒙版会从故事板中消失。在模拟器中,出现角半径(和大概是掩模),但是边框不是。

enter image description here

我也尝试过以编程方式设置它们,但正如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是托管对象。我尽量保持这些东西尽可能简单。

1 个答案:

答案 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?

}

然后您就可以在故事板中执行此操作

enter image description here

希望这有帮助