Detect when my subclass of UIView is added to another UIView

I want my subclass of UIView to automatically position itself when it is added to the view of the parent container.

Is there any way to determine when it will be added and run my positioning code, or do I need to do something like this?

[parentView addSubview:subView]; [setView calcPosition]; 
+4
source share
2 answers

UIView provides the willMoveToSuperview: and didMoveToSuperview . Just override the ones you need to know when a view is added to another view (or later deleted).

+8
source

Write calcPosition methord inside the preview and call it from didMoveToSuperview subview

 - (void)didMoveToSuperview { [super didMoveToSuperview]; [self calcPosition]; } 
0
source

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


All Articles