layoutSubviews的调用-情景再现

2017-06-07  本文已影响44人  姚姚先生

1.在下面情况下会调用layoutSubviews

2.UIView的layoutSubviews调用

SubView*view= [[SubView alloc]initWithFrame:CGRectMake(0,0,100,100)];
view.backgroundColor=[UIColor redColor];
[self.view addSubview:view];
self.sbView= view;
// 添加子控件的时候都会调用
CustomBtn *button = [[CustomBtn alloc] init];
[self.sbView addSubview:button];

2.UIButton的layoutSubviews调用

- (void)viewDidLoad{
 [super viewDidLoad];
CustomBtn*button = [[CustomBtn alloc]init];
[self.view addSubview:button];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// 因为view这时候已经显示了,父控件就不会布局了,所以代码写在这里最好,受干扰的因素最小
CustomBtn*button = [[CustomBtn alloc]init];
[self.view addSubview:button];
}
CustomBtn*button = [[CustomBtn alloc] init];
[button setTitle:@"xxoo" forState:UIControlStateNormal]; 
[self.view addSubview:button];
CustomBtn *button = [[CustomBtn alloc] init];
[button setImage:[UIImage imageNamed:@"xoxo"] forState:UIControlStateNormal];
[self.view addSubview:button];

3.UIScrollView的layoutSubviews调用

// scrollView比较特殊
CustomScrollView *scrollView= [[CustomScrollView alloc]init];
[self.view addSubview:scrollView];
// 因为scrollView有size,所以会调用
CustomScrollView *scrollView= [[CustomScrollView  alloc]initWithFrame:CGRectMake(100,100,300,200)];
[self.view addSubview:scrollView];
scrollView.backgroundColor= [UIColor yellowColor];
scrollView.contentSize= CGSizeMake(0,500);
上一篇下一篇

猜你喜欢

热点阅读