Auto Layout constraint on CALayer IOS
the whole autoresizing business is view-specific. layers don't autoresize.
what you have to do -- in code -- is to resize the layer yourself
e.g.
in a viewController you would do
- (void) viewDidLayoutSubviews { [super viewDidLayoutSubviews]; //if you want superclass's behaviour... // resize your layers based on the view's new frame self.editViewBorderLayer.frame = self.editView.bounds;}
or in a custom UIView you could use
- (void)layoutSubviews { [super layoutSubviews]; //if you want superclass's behaviour... (and lay outing of children) // resize your layers based on the view's new frame layer.frame = self.bounds;}
In Swift 5
, you can try the following solution:
Use KVO, for the path "YourView.bounds" as given below.
self.addObserver(self, forKeyPath: "YourView.bounds", options: .new, context: nil)
Then handle it as given below.
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "YourView.bounds" { YourLayer.frame = YourView.bounds return } super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) }
More info about this here
According to this answer, layoutSubviews
does not get called in all needed cases. I have found this delegate method more effective:
- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if(self != nil) { [self.layer addSublayer:self.mySublayer]; } return self;}- (void)layoutSublayersOfLayer:(CALayer*)layer{ self.mySublayer.frame = self.bounds;}