iOS之layoutSubView的使用

來源:互聯網
上載者:User

轉載自:http://www.cnblogs.com/YouXianMing/p/3897543.html

從百度上搜尋了一下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 = [TestView new];  [self.view addSubview:test];}- (void)test_2{  TestView *test = [TestView new];  test.frame = CGRectMake(0, 0, 100, 100);  [self.view addSubview:test];}- (void)test_3{  /*   解釋:      layoutSubviews這個方法自身無法調用,是被父類添加的時候才執行的方法   */    TestView *test = [TestView new];  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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.