转自: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. CSS动画:Transform中使用频繁的scale,rotate,translate动画

    动画中,skew只是transform中的一种形式的动画,我们还可以学习scale,rotate,translate.这是目前使用比较频繁的属性动作. 1.scale动画的定义:(单位数值) scal ...

  2. ios新特征 ARC详解

    IOS ARC 分类: IOS ARC2013-01-17 09:16 2069人阅读 评论(0) 收藏 举报   目录(?)[+]   关闭工程的ARC(Automatic Reference Co ...

  3. PHP框架、库和软件资源大全(整理篇)

    php的资料 https://github.com/ziadoz/awesome-php Awesome PHP A curated list of amazingly awesome PHP lib ...

  4. 2. SharePoint Online 开发,请联系qq512800530。加好备注。(不要发站内信。。。)

    ///(不要发站内信...) <meta name="keywords" content="SharePoint Online, SP Online, SPO, S ...

  5. 2014年Facebook的开源成就

    2014是Facebook开源硕果丰硕的一年,其开源项目经理詹姆斯·皮尔斯(James Pearce)连续12天发布开源博客文章展示全年该社交网站在此领域取得的成就. 皮尔斯公布的成就包括以下内容: ...

  6. 【Java】对Web Service的理解

    WSDL(Web Service Description Language)是描述Web Service的语言. 你会怎样向别人介绍你的Web service有什么功能,以及每个函数调用时的参数呢?你 ...

  7. Hotel

    poj3667:http://poj.org/problem?id=3667 题目大意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,group ...

  8. mysql explain 解释

     就是10W记录比如全表返回1行  Mysql显示可能是10W oracle是1行   mysql 是预估需要扫描的记录 Oracle 是预估返回的记录

  9. Ubuntu 中使用 谷歌日历

    简介 对于经常使用待办类软件的人来说,谷歌日历是个不错的选择.但每次,都要登录网页去查看,对于我这样的懒人来说似乎麻烦了些. 所以在网上找了个叫做 Calendar Indicator 的软件. 效果 ...

  10. bzoj3275

    容易想到是最小割(最大权独立集)然后每个数拆成两个点,不能同时选的之间连边跑最小割,最后答案=总数-mincut/2 因为这样建图将流量变成了原来的两倍 当然这道题更好的建图方法是分成奇数和偶数两个集 ...