4

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];
4

2 回答 2

8

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

于 2012-10-19T03:37:29.507 回答
0

在子视图中写入方法并从子视图中calcPosition调用它didMoveToSuperview

- (void)didMoveToSuperview
{
    [super didMoveToSuperview];
    [self calcPosition];
}
于 2012-10-19T03:41:36.517 回答