2017-08-06 89 views
2

我正在尝试向@IBDesignable UICollectionViewCell原型添加一个薄的彩色边框。在故事板中,我设置了layer.cornerRadius,layer.masksToBoundslayer.borderWidth用户定义的属性,并且边框按预期显示。当设置layer.borderColor时,UICollectionViewCell边框消失

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? 

} 

和相关部分的UICollectionViewControllerdataSource

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

当然,但它只是一小撮'IBOutlet';没有有趣的代码。 – NRitH

回答

1

您面临的问题是您正在使用Boolean值为layer.borderColor您必须改用Color。但无论如何,这不工作,因为layer.borderColor你需要一个cgColor所以我认为你需要定义一个@IBInspectable VAR为BORDERCOLOR和代码做到这一点

@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

希望这有助于

+0

啊,我对'layer.borderColor'有'Boolean'类型的好的调用。不过,我刚刚上传了错误的屏幕截图 - 我*确实已将它设置为“Color”。当'Color'被指定为用户定义属性中的类型时,IB知道它确实是'CGColor',所以我不认为这是问题所在。但是我可以从你的例子中看出你已经开始工作了,所以我做了其他的事情。你使用的是什么Xcode版本?我在9测试版4. – NRitH

+0

@NRitH您是否尝试添加borderColor作为可检查的属性,正如我在我的答案中所述?,必须工作,无论如何Iam使用Xcode 8.3.2 –

+0

是的,它工作,如果我添加可检查的属性像你建议的那样。我知道我在其他项目上做过这方面的工作;我将把它记入Xcode 9 beta 4中的一个bug。 – NRitH