简体   繁体   中英

Detect when my UIView subclass is added to another UIView

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

Can I somehow detect when it is added and run my positioning code then or do I need to do something like?

[parentView addSubview:subView];
[setView calcPosition];

UIView provides the methods willMoveToSuperview: and didMoveToSuperview . Just override those to know when the view is added to another view (or later removed).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM