1

I know, how to create autolayout constraints with size classes perfectly.

But I am not getting when to call layOutIfNeeded(), setNeedsDisplay(), layOutSubViews(), setUpdateConstraints().

Can someone tell how to properly call this function to update UI after constraints changed.

Another my concern is, when to call only single function out of above and call with other functions.

Saggy
  • 465
  • 5
  • 18
  • check this. http://stackoverflow.com/questions/20609206/setneedslayout-vs-setneedsupdateconstraints-and-layoutifneeded-vs-updateconstra – Badal Shah Apr 04 '16 at 07:59

1 Answers1

0

It must be really clear that your layout is calculated by a routine that is called at specific times at runtime.
It could happen that you need to modify the current layout, for instance changing the constant of a specific constraint. If you just do that you will notice no changes in the UI, this is because the routine is still not called.
What you can do is force the layout routine to be called, and you do that by these two methods:

  • setNeedsLayout : You are telling that the view needs a layout. The next time the routine is called knows that this view need to have a layout refresh
  • layOutIfNeeded(): You don't want to wait the next call and you are telling the system to force layout calculation ASAP

Same thing happen with setNeedsDisplay() and displayIfNeeded(), with the first you tell that a view needs to be rendered again, and with the second you tell do ASAP.
If you are asking yourself why, the reason is performance. Is useless to re-render everything each time, this lazy approach will save system resources.
The methods - setNeedsUpdateConstraints and -updateConstraintsIfNeeded are basically the same concept applied to constraints, the difference is that you will not see any changes in UI until you force a layout, why this methods are useful? because sometimes you need to check after a change in constraint if the layout is still valid without changing the aspect of your UI.

Andrea
  • 26,120
  • 10
  • 85
  • 131
  • So, what would be correct sequece to call these methods. As i am using autolayout with size classes and want to update constraints and display properly for that particular size class only.Rather which methods should i call, in which sequence, in concern of adaptive size classes. – Saggy Apr 04 '16 at 10:48
  • Modify constraints-> setNeedsLayout-> layOutIfNeeded – Andrea Apr 04 '16 at 10:57
  • Right now, i am calling layOutIfNeeded() only; it not updating properly. So, do i need to call both setNeedsLayout() followed by layOutIfNeeded() ? – Saggy Apr 04 '16 at 11:10