转自: 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

3. 如果一个view的frame值改变了,那么它的父类的layoutSubviews也会被执行

[转] 解析LayoutSubviews的更多相关文章

  1. 解析LayoutSubviews

    layoutSubviews作用 layoutSubviews是对subviews重新布局.比如,我们想更新子视图的位置的时候,可以通过调用layoutSubviews方法,既可以实现对子视图重新布局 ...

  2. 笔谈OpenGL ES(二)

    昨晚回家也看了OpenGL ES 2.0 iOS教程的第一篇,对于其中涉及的一些基本知识罗列下,虽然自己做iOS开发一年多了,但是对于一些细节没有注意,真正的把自己当成“应用”工程师了 ,不仅要会用, ...

  3. 字符串处理(正则表达式、NSScanner扫描、CoreParse解析器)-备用

    搜索 在一个字符串中搜索子字符串 最灵活的方法 1 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptio ...

  4. iOS对UIViewController生命周期和属性方法的解析

    目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...

  5. 【iOS开发】iOS对UIViewController生命周期和属性方法的解析

    iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...

  6. 字符串处理(正则表达式、NSScanner扫描、CoreParse解析器)-b

    搜索 在一个字符串中搜索子字符串 最灵活的方法 1 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptio ...

  7. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

  8. .NET Core中的认证管理解析

    .NET Core中的认证管理解析 0x00 问题来源 在新建.NET Core的Web项目时选择“使用个人用户账户”就可以创建一个带有用户和权限管理的项目,已经准备好了用户注册.登录等很多页面,也可 ...

  9. Html Agility Pack 解析Html

    Hello 好久不见 哈哈,今天给大家分享一个解析Html的类库 Html Agility Pack.这个适用于想获取某网页里面的部分内容.今天就拿我的Csdn的博客列表来举例. 打开页面  用Fir ...

随机推荐

  1. html5 存储篇(一)

    localStorage 和 sessionStorage      localStorage 与 sessionStorage的相同点:         (1).都是用于客户端存储的技术,相对于传统 ...

  2. mysql xtrabackup 备份恢复实现,mysql命令备份数据库,打包压缩数据库

    简介 Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具.特点: (1)备份过程快速.可靠 ...

  3. PHP请求第三方接口的函数

    <?php public function HttpGet($url){ $curl = curl_init (); curl_setopt ( $curl, CURLOPT_URL, $url ...

  4. TCP/IP笔记 三.运输层(3)——TCP超时重传算法

    TCP 每发送一个报文段,就对这个报文段设置一次计时器.只要计时器设置的重传时间到但还没有收到确认,就要重传这一报文段 1. 平均往返时延RTT 往返时延:一个报文段发出的时间,以及收到相应的确认报文 ...

  5. C/C++ 基础教程

          自从做IOS后,就比较少用纯C++的方式写代码了,因为Obj-C的代码风格和C++的风格还是有一点区别的.怕自己忘记了C/C++的基础.整理了一些C/C++基础的网站,供大家学习C/C++ ...

  6. mySQL中replace的用法

    MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪   mysql replace实例说明: ...

  7. Android记录4--自定义ToggleButton+用SharedPreferences保存用户配置

    Android记录4--自定义ToggleButton+用SharedPreferences保存用户配置 2013年8月14日Android记录 很多应用都会有用户设置,用户的一些偏好可以由用户来决定 ...

  8. weblogic的ejb远程调用

    这是一篇对EJB远程调用的简单范例.      1.环境:win7  + weblogic 12c + myeclipse8.5      2.目的:实现在myeclispe中对weblogic中EJ ...

  9. OpenStack_Swift源代码分析——Object-auditor源代码分析(1)

    1 Object-auditor 的启动 Object-auditor的启动和object-replicator的启动过程是一样的,首先是运行启动脚本 swift-init object-audito ...

  10. Sql缓存依赖--数据库缓存

    •依赖于文件内容CacheDependency cDep = new CacheDependency(filePath); •依赖于数据库内容(轮询机制/通知机制)一:轮询机制 1.在数据库新建版本表 ...