约束和子层(CALayer)不更新

我在superView中创建了一个左边框

... var leftLayer: CALayer? override public func awakeFromNib() { super.awakeFromNib() self.leftLayer = CALayer() self.leftLayer?.borderColor = UIColor.red.cgColor self.leftLayer?.borderWidth = 1 self.layer.addSublayer(self.leftLayer!) ... 

然后在layoutSubviews中我尝试根据约束更新边框:

 ... override public func layoutSubviews() { super.layoutSublayers(of: layer) leftLayer!.frame = CGRect(x: 0, y: 0, width: 1, height: self.frame.size.height) } ... 

但是子CALayers没有遵循最高的CALayer / UIViews限制。 我已经看过了 ,但无法在superview中添加图层。

你知道如何让子CALayer遵循包含UIView的约束吗?

覆盖视图的frame属性以调整其子图层框架:

 override var frame: CGRect { didSet{ leftLayer!.frame = CGRect(x: 0, y: 0, width: 1, height: self.frame.size.height) } }