layoutSubviews

從百度上搜索了一下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);

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末丹禀,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鞋怀,更是在濱河造成了極大的恐慌双泪,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件密似,死亡現(xiàn)場離奇詭異焙矛,居然都是意外死亡,警方通過查閱死者的電腦和手機残腌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進店門村斟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贫导,“玉大人,你說我怎么就攤上這事蟆盹『⒌疲” “怎么了?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵逾滥,是天一觀的道長峰档。 經(jīng)常有香客問我,道長寨昙,這世上最難降的妖魔是什么讥巡? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮舔哪,結(jié)果婚禮上欢顷,老公的妹妹穿的比我還像新娘。我一直安慰自己捉蚤,他們只是感情好抬驴,可當(dāng)我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著外里,像睡著了一般怎爵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上盅蝗,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天鳖链,我揣著相機與錄音,去河邊找鬼墩莫。 笑死芙委,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的狂秦。 我是一名探鬼主播灌侣,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼裂问!你這毒婦竟也來了侧啼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤堪簿,失蹤者是張志新(化名)和其女友劉穎痊乾,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體椭更,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡哪审,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了虑瀑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片湿滓。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡滴须,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出叽奥,到底是詐尸還是另有隱情扔水,我是刑警寧澤,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布而线,位于F島的核電站铭污,受9級特大地震影響恋日,放射性物質(zhì)發(fā)生泄漏膀篮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一岂膳、第九天 我趴在偏房一處隱蔽的房頂上張望誓竿。 院中可真熱鬧,春花似錦谈截、人聲如沸筷屡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽毙死。三九已至,卻和暖如春喻鳄,著一層夾襖步出監(jiān)牢的瞬間扼倘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工除呵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留再菊,地道東北人。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓颜曾,卻偏偏與公主長得像纠拔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子泛豪,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,446評論 2 359

推薦閱讀更多精彩內(nèi)容