一 , layoutSubviews何时调用的问题

//layoutSubviews何时调用的问题,这个方法是当你需要在调整subview的大小的时候需要重写(我这个翻译不严谨,以下是原文:You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.),但有时候经常指望它被调用的时候没被调用,不希望它被调用的时候被调用了,搞的很上火。根据国外社区一个人帖子,做了总结性翻译。

layoutSubviews在以下情况下会被调用:

1、init初始化不会触发layoutSubviews
2、addSubview会触发layoutSubviews
3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
4、滚动一个UIScrollView会触发layoutSubviews
5、旋转Screen会触发父UIView上的layoutSubviews事件
6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件

7、直接调用setLayoutSubviews。

二,UIView的setNeedsDisplay和setNeedsLayout方法

首先两个方法都是异步执行的。而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到  UIGraphicsGetCurrentContext,就可以画画了。而setNeedsLayout会默认调用layoutSubViews,

就可以  处理子视图中的一些数据。

综上所诉,setNeedsDisplay方便绘图,而layoutSubViews方便出来数据。

三,

在自定义layer中的-(void)drawInContext:方法不会自己调用,只能自己通过setNeedDisplay方法调用。在view中画东西DrawRect:方法在view第一次显示的时候会自动调用

**********************************************************************

在UIView里面有一个方法layoutSubviews,这个方法具体作用是什么呢?

- (void)layoutSubviews;    // override point. called by layoutIfNeeded automatically. As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.

官方文档解释

我们先来看看苹果官方文档的解释:

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.

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.

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.

最后一段说,不要直接调用此方法。如果你想强制更新布局,你可以调用setNeedsLayout方法;如果你想立即数显你的views,你需要调用layoutIfNeeded方法。

layoutSubviews作用

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

layoutSubviews默认是不做任何事情的,用到的时候,需要在自雷进行重写。

layoutSubviews以下情况会被调用

苹果官方文档已经强调,不能直接调用layoutSubviews对子视图进行重新布局。那么,layoutSubviews什么情况下会被调用呢?通过百度搜索,发现以下几种情况layoutSubviews会被调用。

  1. 直接调用setLayoutSubviews。(这个在上面苹果官方文档里有说明)

  2. addSubview的时候。

  3. viewframe发生改变的时候。

  4. 滑动UIScrollView的时候。

  5. 旋转Screen会触发父UIView上的layoutSubviews事件。

  6. 改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。

我简单测试了一下,上面基本都会被调用。 注意:

当view的fram的值为0的时候,`addSubview`也不会调用`layoutSubviews`的。

layoutSubviews方法在对自雷视图进行布局的时候非常方便。可以自己动手,深入理解layoutSubviews的调用机制。

layoutSubviews,setNeedsDisplay的更多相关文章

  1. setNeedsDisplay、layoutSubViews

    UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.而setNeedsDisplay会调 用自动调用drawRect方法,这样可以拿到UIGra ...

  2. UIView之setNeedsDisplay与drawRect 和 setNeedsLayout与layoutSubViews

    1.UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到  UI ...

  3. drawRect setNeedsDisplay layoutSubViews

    drawRect   setNeedsDisplay layoutSubViews 1. drawRect: is invoked automaticall,never call it directl ...

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

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

  5. UIView的setNeedsDisplay和setNeedsLayout

    1,UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到  UI ...

  6. layoutSubviews 与 drawRect

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

  7. setNeedsDisplay,setNeedsLayout

    UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到UIGraph ...

  8. setNeedsDisplay和setNeedsLayout

    1,UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到  UI ...

  9. UIView的layoutSubviews和drawRect

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

随机推荐

  1. linux系统-代码行数计算

    find macc-cometd -type f -name "*.java" -print0 | xargs -0 wc -l

  2. struts2中的addActionError 、addFieldError、addActionMessage的方法

    一 addActionError ①概念addActionError是Action级别的错误消息 ②添加this.addActionError("错误信息"); ③显示<s: ...

  3. 夺命雷公狗ThinkPHP项目之----企业网站12之文章添加的实现

    我们现在就开始写文章添加了,居然是添加当然布列外,我们还是要先讲模版搞定再说被: <!doctype html> <html> <head> <meta ch ...

  4. python入门语法总结 zz

    http://renjie120.iteye.com/blog/680126 1.python是一个解释性语言: 一个用编译性语言比如C或C++写的程序可以从源文件(即C或C++语言)转换到一个你的计 ...

  5. DIV怎样能够垂直居中

    这里只说固定宽高的情况: 1.Top:50%; 2.margin-top:-(height/2); 就这样. 不过很好奇有没有v-align之类的属性可以直接实现.

  6. 160905、c3p0详细配置

    官方文档 : http://www.mchange.com/projects/c3p0/index.html <c3p0-config> <default-config> &l ...

  7. Bootstrap:弹出框和提示框效果以及代码展示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  8. Linux Runtime PM介绍【转】

    转自:http://blog.csdn.net/wlwl0071986/article/details/42677403 一.Runtime PM引言 1. 背景 (1)display的需求 (2)系 ...

  9. (转载)CSV 文件处理 PERL

    http://cn.perlmaven.com/how-to-read-a-csv-file-using-perl http://search.cpan.org/~hmbrand/Text-CSV_X ...

  10. plsql日期乱码

    乱码状况如截图: 控制面板\所有控制面板项\系统\高级系统设置\环境变量, 设置系统变量,变量名:NLS_LANG,变量值:Simplified Chinese_China.AL32UTF8改为SIM ...