呼叫layoutSubviews各種情況分析

jingxianli0922發表於2015-10-27

從百度上搜尋了一下layoutSubviews的用處,以下是搜尋的結果,當然,筆者是會一一驗證的.
1、 init初始化不會觸發layoutSubviews
2、 addSubview會觸發layoutSubviews
3、 設定view的Frame會觸發layoutSubviews,當然前提是frame的值設定前後發生了變化
4、 滾動一個UIScrollView會觸發layoutSubviews
5、 旋轉Screen會觸發父UIView上的layoutSubviews事件
6、 改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件
在開始驗證之前,先看看layoutSubviews到底是啥來著:)
Lays out subviews.
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default implementation uses any constraints you have set to determine the size and position of any subviews.
在iOS5.1或之前的版本中,這個方法什麼也沒幹.這個方法的預設實現是 用引數來設定subviews的尺寸和位置的 .
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.
如果你需要更加精確的佈局,可以在子類裡面重寫這個方法.僅僅在以下情況下:自動佈局達不到你想要效果時你才有必要重寫這個方法.你可以直接設定subviews的尺寸.
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.
你不能直接呼叫這個方法.如果你需要強制layout重新整理,呼叫setNeedsLayout來代替.如果你想要立即重新整理你的view,呼叫layoutIfNeeded
大概總結以下就是:
你不要直接呼叫方法layoutSubviews,如果想要重新整理,請呼叫 setNeedsLayout 或者layoutIfNeeded
好了,開始驗證:)
現在提供繼承至UIView的類如下:

////  TestView.h//  

LayoutSubviews////  Copyright (c) 2014年 Y.X. All rights reserved.

//#import <UIKit/UIKit.h>


@interface TestView : UIView

@end

////  TestView.m//  LayoutSubviews////  Copyright (c) 2014年 Y.X. All rights reserved.

//#import"TestView.h"


@implementation TestView

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self)
  {
    NSLog(@"initWithFrame:%@" ,NSStringFromCGRect(frame));
  }
  return self;
}

- (void)layoutSubviews
{
  NSLog(@"layoutSubviews %@", self);
  [super layoutSubviews];
}

@end
測試程式碼:

////  RootViewController.m//  

LayoutSubviews////  Copyright (c) 2014年 Y.X. All rights reserved.//

#import"RootViewController.h"

#import"TestView.h"


@interface RootViewController ()

@property (nonatomic, strong) NSTimer  *timer;
@property (nonatomic, strong) TestView *largeView;
@property (nonatomic, strong) TestView *smallView;

@end

@implementation RootViewController


- (void)viewDidLoad
{
  [super viewDidLoad];

  // 1、init初始化不會觸發layoutSubviews [正確的]  

// 2、addSubview會觸發layoutSubviews [不完全正確,當frame為0時是不會觸發的]  

// 3、設定view的Frame會觸發layoutSubviews,當然前提是frame的值設定前後發生了變化 [正確]  

//    [self test_1];

//     [self test_2];

//     [self test_3];    

// 4、滾動一個UIScrollView會觸發layoutSubviews[錯誤,不用滾動就會觸發]

//     [self test_4];   

// 5、改變一個UIView大小的時候也會觸發父UIView上的layoutSubviews事件  

[self test_5];

}


- (void)test_1
{
  /* 
   解釋:
   
   走了initWithFrame:方法,但是又有frame值為{{0, 0}, {0, 0}},並不需要繪製任何的東西,
   所以即使新增了test,也沒必要繪製它,同時也驗證了addSubview會觸發layoutSubviews是錯
   誤的,只有當被新增的view有著尺寸的時候才會觸發layoutSubviews
   */    TestView*test = [TestViewnew];
  [self.view addSubview:test];
}

- (void)test_2
{
  TestView *test = [TestViewnew];
  test.frame= CGRectMake(0,0,100,100);
  [self.view addSubview:test];
}

- (void)test_3
{
  /*   解釋:
   
   layoutSubviews這個方法自身無法呼叫,是被父類新增的時候才執行的方法

   */    

  TestView*test = [TestViewnew];

  test.frame= CGRectMake(0,0,50,50);
  UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,50)];
  [test addSubview:showView];
}

- (void)test_4
{
  CGRect rect      = self.view.bounds;
  CGFloat height= rect.size.height;
  CGFloat width = rect.size.width;
    UIScrollView*rootScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  NSArray *data                      = @[@"",@"",@"",@""];
  [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    TestView*tmp                = [[TestView alloc] initWithFrame:CGRectMake(width*idx,0,
                                      width, height)];
    [rootScroll addSubview:tmp];
  }];
  rootScroll.contentSize  = CGSizeMake(width * data.count, height);
  [self.view addSubview:rootScroll];
}

- (void)test_5
{
  _timer = [NSTimer scheduledTimerWithTimeInterval:1.f
                        target:self
                      selector:@selector(timerEvent:)
                      userInfo:nil
                       repeats:YES];
  _largeView= [[TestView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview:_largeView];
    _smallView= [[TestView alloc] initWithFrame:CGRectMake(0,0,100,100)];
  [_largeView addSubview:_smallView];
}

- (void)timerEvent:(id)sender
{
  _smallView.frame= CGRectMake(arc4random()%100 + 20,
                  arc4random()%100 + 20,
                  arc4random()%100 + 20,
                  arc4random()%100 + 20);
  NSLog(@"_smallView %@", _smallView);
  NSLog(@"_smallView %@", _largeView);
}

@end
測試後的結論是這樣子的:
1. 一個view是不能夠自己呼叫layoutSubviews,如果要呼叫,需要呼叫 setNeedsLayout或者 layoutIfNeeded
2. 如果view的frame值為0,即使被新增了耶不會呼叫layoutSubviews

3. 如果一個view的frame值改變了,那麼它的父類的layoutSubviews也會被執行

相關文章