Search code examples
iphoneobjective-ciosuiviewaddsubview

Trigger to addSubview method


I'm looking for a way - how to detect: my UIView was added as subview to some other view to adapt it's frame to superview.

F.e I should put my UIView to the bottom of parent view (but I don't know which parent will be... :| )

The only way that I can see is override parent addSubview method, but I don't have access to it.

I would like to have delegate method like:

- (void)viewDidAddedAsSubview:(UIView)parentView

or smth like that

Maybe, somebody knows how to do this.

Thanks a lot


Solution

  • If the view you are adding is a custom UIView class, then override the willMoveToSuperview: or the didMoveToSuperView method. Take into account that these are called when the view is removed as well.

    If the view you are adding is not custom UIView then you can add an observer on the view's superview property.