0

Can anyone tell that what these 4 methods definition and what they do

 1. setNeedLayout
 2. setNeedDisplay
 3. layoutSubViews
 4. layoutIfNeeded.

I googled a lot can't find a good answer.

Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
  • as I remember calling just `setNeedsLayout ` will ensure calling `layoutIfNeeded ` and `layoutSubViews ` also – Tj3n Feb 10 '17 at 11:26
  • Please add as answer it will help other too :) – Abhishek Thapliyal Feb 10 '17 at 11:26
  • In the right hand got similar answer, probably you havent searched correctly? like [this](http://stackoverflow.com/questions/20609206/setneedslayout-vs-setneedsupdateconstraints-and-layoutifneeded-vs-updateconstra) or [this](http://stackoverflow.com/questions/2807137/what-is-the-relationship-between-uiviews-setneedslayout-layoutifneeded-and-lay?rq=1), their answer have details – Tj3n Feb 10 '17 at 11:27
  • i go through both links already i need a simple explanations – Abhishek Thapliyal Feb 10 '17 at 11:30
  • 1
    Why don't you just command+click on those methods and see inside. :) @AbhishekThapliyal – iPeter Feb 10 '17 at 11:35
  • 1
    Usually you only need to call `layoutIfNeeded` if you make changes to constraint to make the view update it, also can put it in `UIView.animated` to animate the whole layout update – Tj3n Feb 10 '17 at 11:38
  • @iPeter : Thanks for this idea :) – Abhishek Thapliyal Feb 10 '17 at 11:38
  • check this ans: http://stackoverflow.com/a/2931712/4831524 – Antony Raphel Feb 10 '17 at 12:47

1 Answers1

4
  1. setNeedsLayout is useful for calling the 4th one layoutIfNeeded. It makes it really needed.
  2. setNeedsDisplay makes drawRect method called at the next run loop iteration and is not really related to the rest of three methods in question
  3. layoutSubviews performs actual layout of the view's subviews
  4. layoutIfNeeded calls layoutSubviews when it is needed. You can make it needed by using the 1st method setNeedsLayout
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161