從百度上搜索了一下layoutSubviews的用處,以下是搜索的結(jié)果,當(dāng)然,筆者是會一一驗證的.
1、init初始化不會觸發(fā)layoutSubviews
2挠说、addSubview會觸發(fā)layoutSubviews
3棕孙、設(shè)置view的Frame會觸發(fā)layoutSubviews制市,當(dāng)然前提是frame的值設(shè)置前后發(fā)生了變化
4、滾動一個UIScrollView會觸發(fā)layoutSubviews
5犯眠、旋轉(zhuǎn)Screen會觸發(fā)父UIView上的layoutSubviews事件
6嚷硫、改變一個UIView大小的時候也會觸發(fā)父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或之前的版本中,這個方法什么也沒干.這個方法的默認(rèn)實現(xiàn)是用參數(shù)來設(shè)定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.
如果你需要更加精確的布局,可以在子類里面重寫這個方法.僅僅在以下情況下:自動布局達(dá)不到你想要效果時你才有必要重寫這個方法.你可以直接設(shè)置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.
你不能直接調(diào)用這個方法.如果你需要強制layout刷新,調(diào)用setNeedsLayout來代替.如果你想要立即刷新你的view,調(diào)用layoutIfNeeded
大概總結(jié)以下就是:
你不要直接調(diào)用方法layoutSubviews,如果想要刷新,請調(diào)用setNeedsLayout或者layoutIfNeeded
好了,開始驗證:)
現(xiàn)在提供繼承至UIView的類如下:
////TestView.h//LayoutSubviews////Copyright (c) 2014年 Y.X. All rights reserved.//#import @interfaceTestView : UIView@end
////TestView.m//LayoutSubviews////Copyright (c) 2014年 Y.X. All rights reserved.//#import"TestView.h"@implementationTestView- (id)initWithFrame:(CGRect)frame{? ? self =[super initWithFrame:frame];if(self)? ? {? ? ? ? NSLog(@"initWithFrame:%@",NSStringFromCGRect(frame));? ? }returnself;}- (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"@interfaceRootViewController ()@property (nonatomic, strong) NSTimer? *timer;@property (nonatomic, strong) TestView? *largeView;@property (nonatomic, strong) TestView? *smallView;@end@implementationRootViewController- (void)viewDidLoad{? ? [super viewDidLoad];//1检访、init初始化不會觸發(fā)layoutSubviews [正確的]//2、addSubview會觸發(fā)layoutSubviews [不完全正確,當(dāng)frame為0時是不會觸發(fā)的]//3仔掸、設(shè)置view的Frame會觸發(fā)layoutSubviews烛谊,當(dāng)然前提是frame的值設(shè)置前后發(fā)生了變化 [正確]//[self test_1];//[self test_2];//[self test_3];//4、滾動一個UIScrollView會觸發(fā)layoutSubviews[錯誤,不用滾動就會觸發(fā)]//[self test_4];//5嘉汰、改變一個UIView大小的時候也會觸發(fā)父UIView上的layoutSubviews事件[self test_5];}- (void)test_1{/*解釋:? ? ? ? ? 走了initWithFrame:方法,但是又有frame值為{{0, 0}, {0, 0}},并不需要繪制任何的東西,? ? 所以即使添加了test,也沒必要繪制它,同時也驗證了addSubview會觸發(fā)layoutSubviews是錯? ? 誤的,只有當(dāng)被添加的view有著尺寸的時候才會觸發(fā)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這個方法自身無法調(diào)用,是被父類添加的時候才執(zhí)行的方法*/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);
}