My understanding is, after execute setNeedsLayout
will mark the view as "dirty" so that in the next rendering cycle, the view will be be re-layout. layoutIfNeeded
will force to trigger re-layout immediately if the view is marked as "dirty".
However, even layoutIfNeeded
immediately start to layout, it does not mean that the code after layoutIfNeeded
will be waiting until it finishes. The render part is an async operation, it will be handled by core graphic through runloop.
myView.setNeedsLayout()
myView.layoutIfneeded()
print(myView.frame)
My point is, the printing of frame above may not always get correct frame, since the render operation is an async task, am I right?
Some context:
- myView is render finished.
- one of its subview's height constraint get changed due to some async data task.
- execute above setNeedsLayout and layoutIfNeed like mentioned above.