layoutSubviews,setNeedsDisplay
一 , 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会被调用。
直接调用
setLayoutSubviews。(这个在上面苹果官方文档里有说明)addSubview的时候。当
view的frame发生改变的时候。滑动
UIScrollView的时候。旋转
Screen会触发父UIView上的layoutSubviews事件。改变一个
UIView大小的时候也会触发父UIView上的layoutSubviews事件。
我简单测试了一下,上面基本都会被调用。 注意:
当view的fram的值为0的时候,`addSubview`也不会调用`layoutSubviews`的。
layoutSubviews方法在对自雷视图进行布局的时候非常方便。可以自己动手,深入理解layoutSubviews的调用机制。
layoutSubviews,setNeedsDisplay的更多相关文章
- setNeedsDisplay、layoutSubViews
UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.而setNeedsDisplay会调 用自动调用drawRect方法,这样可以拿到UIGra ...
- UIView之setNeedsDisplay与drawRect 和 setNeedsLayout与layoutSubViews
1.UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到 UI ...
- drawRect setNeedsDisplay layoutSubViews
drawRect setNeedsDisplay layoutSubViews 1. drawRect: is invoked automaticall,never call it directl ...
- UIView的layoutSubviews和drawRect方法何时调用
首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...
- UIView的setNeedsDisplay和setNeedsLayout
1,UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到 UI ...
- layoutSubviews 与 drawRect
layoutSubviews总结 ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size- (void)sizeToFit——————- - (voi ...
- setNeedsDisplay,setNeedsLayout
UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到UIGraph ...
- setNeedsDisplay和setNeedsLayout
1,UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到 UI ...
- UIView的layoutSubviews和drawRect
原文: UIView的layoutSubviews和drawRect UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.setNeedsDis ...
随机推荐
- sql 存储过程 分页
ALTER PROCEDURE [dbo].[BrokerToLenderDataShow2]@Where VARCHAR(200), --查询条件 不含'where'字符,如id>10 and ...
- 夺命雷公狗---微信开发59----在线点播电影网1之ckplayer播放器
我们节课程就要开始写一个小项目了,这项目主要是写一个在线点播电影影网的,我们用到的播放器是ckplayer ckplayer基本介绍: ckplayer的全称是:超酷flv播放器,他是一款用于网页上播 ...
- clock gate cell
clock gate的cell多采用latch的形式,来实现,尽可能避免glitch的产生. 可以的verilog建模方式: module cell_ckgate(TE,E,CP,Q) input ...
- bug的约束
1.bug的标题:主模块-子模块-页面-功能描述-bug的描述
- Android webview实现上传图片的效果(图片压缩)
mainactivity代码 package com.bwie.webviewupload; import java.io.ByteArrayInputStream; import java.io.B ...
- 程序员PC选购
程序员PC选购[转载] http://www.cnblogs.com/legendtao/p/4631150.html 好马配上好鞍,自然事半功倍.一台好的PC能给你更好的工作娱乐体验~~(卧槽,感觉 ...
- MySQL OnlineDDL
参考资料: http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html http://www.mysqlperfo ...
- iOS 学习笔记 五 (2015.03.17)使用storyBoard进行tableview的跳转
方法一: 点击tableviewCell后,按住ctrl键拖拽至想要跳转的新的界面.这样跳转的结果是,点击tableview中的任何一行都会跳转到新的界面.可以通过控制cell的 属性 userInt ...
- PayPal网站付款标准版(for PHP)
简单整理一下PHP项目整合PayPal支付功能. 一.表单的构建: <form method="post" name="form" action=&quo ...
- JVM学习笔记(二)------Java代码编译和执行的整个过程【转】
转自:http://blog.csdn.net/cutesource/article/details/5904542 版权声明:本文为博主原创文章,未经博主允许不得转载. Java代码编译是由Java ...