iOS干货iOS学习ioS技术收藏文章

layoutSubviews在什么情况下调用

2016-01-28  本文已影响7016人  coderYJ
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];
   - (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];
// 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);
上一篇下一篇

猜你喜欢

热点阅读