setNeedsLayout 与 setNeedsUpdateConstraints 关系
来源:互联网 发布:大众网络报 倒闭 编辑:程序博客网 时间:2024/06/02 06:40
记录一下,方便查阅:http://stackoverflow.com/questions/20609206/setneedslayout-vs-setneedsupdateconstraints-and-layoutifneeded-vs-updateconstra
I know that the auto layout chain consists in basically 3 different process.
- updating constraints
- layout views (here is where we get calculation of frames)
- display
What's is not totally clear to me is the inner difference between -setNeedsLayout
and -setNeedsUpdateConstraints
. From Apple Docs:
setNeedsLayout
Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.
setNeedsUpdateConstraints
When a property of your custom view changes in a way that would impact constraints, you can call this method to indicate that the constraints need to be updated at some point in the future. The system will then call updateConstraints as part of its normal layout pass. Updating constraints all at once just before they are needed ensures that you don’t needlessly recalculate constraints when multiple changes are made to your view in between layout passes.
When I want to animate a view after modifying a constraint and animate the changes I usually call for instance:
[UIView animateWithDuration:1.0f delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{ [self.modifConstrView setNeedsUpdateConstraints]; [self.modifConstrView layoutIfNeeded]; } completion:NULL];
I've found out that if I use -setNeedsLayout
instead of -setNeedsUpdateConstraints
everything work as expected, but if I change -layoutIfNeeded
with -updateConstraintsIfNeeded
, the animation won't happen.
I've tried to make my own conclusion:
-updateConstraintsIfNeeded
only update constraints but doesn't force the layout to come into the process, thus original frames are still preserved-setNeedsLayout
calls also-updateContraints
method
So when is ok to use one instead of the other? and about the layout methods, do I need to call them on the view that has a change in a constraint or on the parent view?
The basic scheme is:
setNeedsUpdateConstraints
makes sure a future call toupdateConstraintsIfNeeded
callsupdateConstraints
.setNeedsLayout
makes sure a future call tolayoutIfNeeded
callslayoutSubviews
.
When layoutSubviews
is called, it also calls updateConstraintsIfNeeded
, so calling it manually is rarely needed in my experience. In fact, I have never called it except when debugging layouts.
Updating constraints using setNeedsUpdateConstraints
is pretty rare too, objc.io–a must read about autolayouts–says:
If something changes later on that invalidates one of your constraints, you should remove the constraint immediately and call setNeedsUpdateConstraints. In fact, that’s the only case where you should have to trigger a constraint update pass.
In addition, in my experience, I have never had to invalidate constraints, and not set the setNeedsLayout
in the next line of the code, because new constraints pretty much are asking for a new layout.
The rules of thumb are:
- If you manipulated constraints directly, call
setNeedsLayout
. - If you changed some conditions (like offsets or smth) which would change constraints in your overridden
updateConstraints
method (a recommended way to change constraints, btw), callsetNeedsUpdateConstraints
, and most of the time,setNeedsLayout
after that. - If you need any of the actions above to have immediate effect—e.g. when your need to learn new frame height after a layout pass—append it with a
layoutIfNeeded
.
Also, in your animation code, I believe setNeedsUpdateConstraints
is unneeded, since constraints are updated before the animation manually, and the animation only re-lays-out the view based on differences between the old and new ones.
Below is the diagram of a typical UIView cycle which explains other behaviors
- I've found out that if I use -setNeedsLayout instead of -setNeedsUpdateConstraints everything work as expected, but if I change -layoutIfNeeded with -updateConstraintsIfNeeded, the animation won't happen.
updateConstraints
typically doesn't do anything. It just resolves constraints it doesn't apply them till layoutSubviews
is called. So animation does requires a call to layoutSubviews
.
- setNeedsLayout calls also -updateContraints method
No this is not necessary. If your constraints haven't been modified UIView will skip call to updateConstraints
. You need to explicitly call setNeedsUpdateConstraint
to modify constraints in the process.
In order to call updateConstraints
you need to do the following :
[view setNeedsUpdateConstraints]; [view setNeedsLayout]; [view layoutIfNeeded];
- setNeedsLayout 与 setNeedsUpdateConstraints 关系
- setNeedsLayout与layoutSubviews
- setNeedsDisplay与setNeedsLayout
- setNeedsLayout与layoutIfNeeded
- SetNeedsLayout
- setNeedsDisplay,setNeedsLayout,layoutSubviews,drawRect的调用关系
- setNeedsDisplay与setNeedsLayout的区别
- UIView的setNeedsLayout,layoutIfNeeded,layoutSubviews 方法之间的关系解释
- ios setNeedsDisplay和setNeedsLayout的区别与用处
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- UIView 的 setNeedsLayout、 layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- 对 UIView 的 setNeedsLayout、 layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- iOS UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled
- Minimum Size Subarray Sum
- VUI-百度语音之Rest接口python学习笔记0
- Android 公私钥加密
- 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
- setNeedsLayout 与 setNeedsUpdateConstraints 关系
- Unity水面Shader
- 结构思考力实践+读书习作一 开篇有益
- 用一根网线就可以连接笔记本和树莓派,不需要显示器
- JSP中 <c:if test=""> 如何判断?
- php分页技术
- 你所不知的 CSS ::before 和 ::after 伪元素用法
- Java String类常用方法介绍(1)
- matplotlib——pyplot和pylab区别