CALayer Center in UIView

How to center CALayer in a UIView ?

I have the following code, but it seems like it cannot get CALayer centered in the view. It seems that the image of the layer ultimately has the lower right corner in the center of the screen.

UIView code:

 - (void)awakeFromNib { testLayer = [CALayer layer]; testLayer = [[UIScreen mainScreen] scale]; testLayer = kCAGravityCenter; testLayer.contents = (id) self.testImage.CGImage; [testLayer setPosition:self.center]; [self.layer addSublayer:testLayer]; } - (void)layoutSubviews { testLayer.frame = self.bounds; } 
+4
source share
1 answer

You must set the position in layoutSubviews. You do it too early, I think the center is not yet true.

0
source

Source: https://habr.com/ru/post/1500824/


All Articles