iOS开发之layoutSubviews的作用和调用机制
来源:互联网 发布:用c语言编写计算器 编辑:程序博客网 时间:2024/05/08 04:37
在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
的调用机制。
0 0
- iOS开发之layoutSubviews的作用和调用机制
- iOS开发之layoutSubviews的作用和调用机制
- iOS开发之layoutSubviews的作用和调用机制
- iOS开发之layoutSubviews的作用和调用机制
- iOS开发之layoutSubviews的作用和调用机制
- iOS开发之layoutSubviews的作用和调用机制
- iOS开发之layoutSubviews的作用和调用机制
- iOS开发 layoutSubviews的作用和调用机制
- iOS layoutSubviews的作用和调用机制
- iOS开发layoutSubviews的作用及调用机制
- iOS开发之drawRect的作用和调用机制
- iOS开发之drawRect的作用和调用机制
- iOS layoutSubviews的调用
- 开发小结之layoutSubviews调用
- 谈谈iOS的layoutSubviews机制
- iOS中layoutSubviews的调用
- iOS UIView的layoutSubviews和drawRect方法何时调用
- iOS开发笔记--什么时候调用layoutSubviews
- Cocos2d-x 3.3中MoveTo和MoveBy用法
- Maven生命周期
- 数据结构实验之二叉树的建立与遍历
- Algorithms—48.Rotate Image
- 先验概率、似然函数、后验概率、贝叶斯公式
- iOS开发之layoutSubviews的作用和调用机制
- 缩放图片,解决bitmap 内存溢出out of memory的问题
- Scala练习(一)
- Hiwork实现全端覆盖——联合CSDN和码农周刊给各位送书啦!!
- Appium 处理滑动
- 状态栏隐藏
- JNI学习之步步深入四--皇后的祝福
- Android旋转动画
- IOS:UITextField输入中文时限制长度