In a UIView layout, what is an “update loop”?

According to the documentation for -[UIView setNeedsLayout] :

Since this method does not cause an immediate update, but instead expects the next update cycle, you can use it to cancel the layout of several views before any of these views is updated. This allows you to consolidate all layout updates in one update cycle, which is usually better for performance.

That sounds great - but when I use setNeedsLayout without calling layoutIfNeeded , I find that my control is not exhausted. I was hoping that the “update cycle” would happen before the control was shown, but I think this is not how it works. So what is an “update cycle”? When does this happen?

+4
source share
5 answers

An “update cycle” occurs at the end of the current cycle cycle cycle .

setNeedsLayout should be called in the main thread (main runloop).

+3
source

What is strange, I use a lot of custom materials, which I change and call "[self setNeedsLayout]", and I never had to call "layoutIfNeeded" ...

Are you sure your drawRect is fine and has no problems? Perhaps its data is not ready before you call "setNeedsLayout".

The first answer to the next thread may help you.

0
source

Check out this link. This can help you. And if you could provide sample code. It would be helpful.

0
source

I don’t think there is such an obvious error in UIKit. As you, I would check to see if my code cancels all the basic methods. Especially in the hierarchy of parents of the problem object. If this does not help, I would replace my own parent view with some standard views to see if the problem persists. This will help isolate the problem.

0
source

Instead, use layoutIfNeeded, which triggers an immediate layout update.

setNeedsLayout simply paints the layout to the handler, which runs until the next update cycle. refer to: http://www.iosinsight.com/setneedslayout-vs-layoutifneeded-explained

0
source

Source: https://habr.com/ru/post/1415413/


All Articles