Is there a method called viewDidLayoutSubviews?

I need to add CALayer to UILabel but only after UILabel has changed its frame. The UILabel changes its frame in the method viewDidLayoutSubviews

, and if I add a CALayer, it creates multiple instances of CALayers as it gets viewDidLayoutSubviews

called multiple times. I tried to remove all CALayers before adding a new one, but sometimes that also removes the text. I just used self.eventAttending!.layer.sublayers = nil

to set subwords to null.

So I think the simplest answer to my problem is to simply add the CALayer after the frame has been set to viewDidLayoutSubviews

, which brings me to my question: which method is called after viewDidLayoutSubviews

?

+3


source to share


2 answers


There is no method that is always called after viewDidLayoutSubviews

, that is called during the creation process, but it can also be called when things happen to your view, such as adding a new subview.



Your best bet is to create a reference variable on your layer so you can remove it, adjust it as needed, or just check if that reference variance is not null and then don't recreate it.

+7


source


Add a layer or subview if you need and when you need it. Let layoutSubviews do its job.

If you can only add your layer after the compose process is complete, you are doing the wrong thing during the compose step.



Correct your design, this is the best and easiest way.

+3


source







All Articles