简体   繁体   中英

Constraints and SubLayers(CALayer) not updating

I have created a left border in a 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!)
...

and then in layoutSubviews I try to update the border acording to constraints:

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

}
...

But sub CALayers is not following the top CALayer/UIViews constraints . I have seen this , but unable to add layers to a superview.

Do you know how to have a sub CALayer follow the constraints of the containing UIView?

Override the view's frame property to adjust its sub layers frame:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM