Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Detect when my UIView is add in other view

CustomView *customView = [...]; [self.view addSubview:customView]; 

I need to detect in my CustomView class when it is added in other views or when my superview changes.

like image 672
Jeremy Grenier Avatar asked Jul 19 '12 09:07

Jeremy Grenier


People also ask

How do I know if a ViewController is visible?

The view's window property is non-nil if a view is currently visible, so check the main view in the view controller: Invoking the view method causes the view to load (if it is not loaded) which is unnecessary and may be undesirable. It would be better to check first to see if it is already loaded.

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.


1 Answers

You can use willMoveToSuperview: and didMoveToSuperview to detect when the view is moved around. layoutSubviews will be called when the superview changes frame.

like image 98
Hampus Nilsson Avatar answered Oct 02 '22 21:10

Hampus Nilsson