转自:http://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/

It’s important to optimize any UIView layoutSubviews method you create, as it can be frequently called, and has the potential for creating recursion (triggering a setNeedsLayout from layoutSubviews can create a loop that will grossly affect your apps performance). Layout subviews is called once per run loop on any view that has had setNeedsLayout or setNeedsDisplayWithRect: called on it. So in addition to any time you manually call these methods, it can be useful to know when the UI framework calls setNeedsLayout/setNeedsDisplay as this will trigger layoutSubviews.

For this purpose, I will define a few view relationships:

  • View1 – UIView class, root view for examples
  • View1.1 – UIScrollView class, subview of View1
  • View1.1.1 – UIView class, subview of View1.1 (No autoresize mask)
  • View1.1.2 – UIView class, another subview of View1.1 (Autoresize mask – flexible width)

I then ran the following tests.  An X means the view was layed out

From this I surmise the following:

  • init does not cause layoutSubviews to be called (duh)
  • addSubview causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target view
  • setFrame intelligently calls layoutSubviews on the view having it’s frame set only if the size parameter of the frame is different
  • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and it’s superview
  • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
  • removeFromSuperview – layoutSubviews is called on superview only (not show in table)

When does layoutSubviews get called?的更多相关文章

  1. UIView的layoutSubviews和drawRect方法何时调用

    首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...

  2. 关于layoutSubviews

    layoutSubviews 是什么? 设定subviews的尺寸和位置,如果要精确布局,可以在子类里重写此方法.不能直接调用此方法,如果想强制layout刷新,调用setNeedsLayout来代替 ...

  3. layoutSubviews 与 drawRect

    layoutSubviews总结 ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size- (void)sizeToFit——————- - (voi ...

  4. layoutSubviews方法需要被调用的情况有哪些

    layoutSubviews方法:这个方法,默认没有做任何事情,需要子类进行重写 layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews 但是是用i ...

  5. iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View

    一.自定义构造方法 有时候需要快速创建对象,可以自定义构造方法 + (instancetype)shopView { return [[self alloc] init]; } - (instance ...

  6. UIView的layoutSubviews和drawRect

    原文: UIView的layoutSubviews和drawRect UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.setNeedsDis ...

  7. layoutSubviews #pragma mark -

    >>>layoutSubviews: layoutSubviews是对sbuviews的重新布局,比如,我们想更新子视图的位置,可以通过调用layoutSubviews方法(不能直接 ...

  8. layoutSubviews总结

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ——————- - (void)layoutSubview ...

  9. 触发layoutSubviews的条件

    1. init初始化不会触发layoutSubviews 2. addSubview会触发layoutSubviews 3. 设置view的Frame会触发layoutSubviews,当然前提是fr ...

  10. UIView的layoutSubviews和drawRect方法何时调用 ———转

    转自:http://jianyu996.blog.163.com/blog/static/112114555201305113018814/ 首先两个方法都是异步执行.layoutSubviews方便 ...

随机推荐

  1. 兼容IE6的页面底部固定层CSS代码

    有时候当我们需要把一个元素固定在页面的某个部位,一般都是用css中的“position:fixed;”方法来解决,但是IE6不支持fixed,所以今天分享一个兼容IE6的页面底部固定层CSS代码.如下 ...

  2. Access自动编号的初始值设置及重置编号 转

    方法如下: ALTER TABLE tableName ALTER COLUMN Id COUNTER (100, 5) 其中:tableName为要修改的表名,Id为自动编号列,100为初始值,5为 ...

  3. SQL 左外连接查询 将右表中的多行变为左表的一列或多列

    示例: --行列互转 /**************************************************************************************** ...

  4. access_token的获取2

    概述 access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. access_token的存储至少要保留512个字符空间.acces ...

  5. linux根目录下各文件的作用

    各文件详列:   /bin 存放常用命令的目录(二进制可执行命令)    /dev 设备特殊文件    /etc 存放配置相关的文件(系统管理和配置文件)    /etc/rc.d 启动的配置文件和脚 ...

  6. PHP对象类型在内存中的分配

    对象类型和整型.字符串等类型一样,也是PHP中的一种数据类型.都是在程序中用于存储不同类型数据使用的,在程序运行时它的每部分内容都要先加载到内存中再被使用.那么对象类型的数据在内存中是如何分配的呢?先 ...

  7. 项目知识点.Part1

    1. storyboard中添加scrollview: 先添加scrollView,进行约束 添加View 进行约束 相对于scrollView 如果水平滑动:设置vertically in Cont ...

  8. UBOOT的多支持性与可裁剪性

    UBOOT功能强大,适用于多种操作系统,多种处理器架构. 在阅读它的源码时,可以看到cpu目录有各种处理器,而board目录有各种开发板.但是,对于一个特定的实验平台,例如TQ2440开发板,它用到的 ...

  9. sql 使用 FOR XML PATH实现字符串拼接

    sql中经常需要把多个行数据合成一行下面是利用 FOR XML PATH来实现的简单介绍. 1,把图一的转换为图二: SELECT articleID, (),tagID)+',' FROM arti ...

  10. BZOJ 1823 满汉全席

    Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...