我已经在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)
}
...
但是sub CALayers没有遵循CALayer / UIViews的最高约束。我见过this,但是无法将图层添加到 super 视图。
您知道如何让子CALayer遵循包含UIView的约束吗?
最佳答案
覆盖视图的frame属性以调整其子图层的frame:
override var frame: CGRect {
didSet{
leftLayer!.frame = CGRect(x: 0,
y: 0,
width: 1,
height: self.frame.size.height)
}
}
关于ios - 约束和SubLayers(CALayer)未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45750882/