CALayers has not changed according to the change in the boundaries of the UIView. What for?

I have a UIView that has about 8 different CALayer sublayers added to its layer. If I change the boundaries of the view (animated), then the view itself is compressed (I checked it with backgroundColor ), but the size of the sublayers remains unchanged .. p>

How to solve this?

+71
ios objective-c iphone calayer uiview
Mar 23
source share
7 answers

I used the same approach as Solin, but there is a typo in this code. The method should be:

 - (void)layoutSubviews { // resize your layers based on the view new bounds mylayer.frame = self.bounds; } 

For my purposes, I always wanted the sublevel to be the full size of the parent view. Put this method in the view class.

+109
Nov 04 '10 at 15:37
source share

Since CALayer on the iPhone does not support layout managers, I think you need to make your main layer of your view a subclass of CALayer, in which you override layoutSublayers to set the frames of all sublayers. You must also override the view +layerClass method to return the class of your new CALayer subclass.

+22
Mar 24 '10 at 0:40
source share

I used this in a UIView.

 -(void)layoutSublayersOfLayer:(CALayer *)layer { if (layer == self.layer) { _anySubLayer.frame = layer.bounds; } super.layoutSublayersOfLayer(layer) } 

It works for me.

+9
Feb 27 '15 at 3:39
source share

I had the same problem. In the custom view layer, I added two more sublayers. To resize the sublayers (every time the borders of the user view change), I implemented the layoutSubviews method of my user view; inside this method, I just update each sublayer frame to fit the current boundaries of my subview level.

Something like that:

 -(void)layoutSubviews{ //keep the same origin, just update the width and height if(sublayer1!=nil){ sublayer1.frame = self.layer.bounds; } } 
+8
Jul 28 '10 at 8:21
source share

version of Swift 3

In the custom cell add the following lines

Announce first

 let gradientLayer: CAGradientLayer = CAGradientLayer() 

Then add the following lines

 override func layoutSubviews() { gradientLayer.frame = self.YourCustomView.bounds } 
+3
Jun 08 '17 at 10:38 on
source share

2017

The literal answer to this question is:

"CALayers has not changed to match the boundaries of the UIView. Why?"

is that better or worse

needsDisplayOnBoundsChange

the default value is false in CALayer .

decision

 class CircularGradientViewLayer: CALayer { override init() { super.init() needsDisplayOnBoundsChange = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override open func draw(in ctx: CGContext) { go crazy drawing in .bounds } } 

In fact, I am directing you to this QA

stack overflow

which explains what the hell critical setting is contentsScale ; you usually need to set this when you set requireDisplayOnBoundsChange.

+1
Dec 11 '17 at 17:56 on
source share

As [Ole] wrote, CALayer does not support automation in iOS. Therefore, you must customize the layout manually. My option was to adjust the layer frame inside (iOS 7 and earlier)

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

or (with iOS 8)

 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinato 
0
Mar 01 '15 at 12:59 on
source share



All Articles