7

I am going through a tutorial that mentions the following method:

-(void)layoutSubviews

located in CustomerCell.m

When is this method called during the app execution? Is it automatically invoked? I dont see any callback.

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
  • Possible duplicate of [When is layoutSubviews called?](https://stackoverflow.com/questions/728372/when-is-layoutsubviews-called) – Wayne May 09 '18 at 23:09

3 Answers3

13

You will find your answer here

EDIT: copied directly from the blog:

  • init does not cause layoutSubviews to be called (duh)
  • addSubview causes layoutSubviews to be called on the view being added, the view it’s being added to (target view), and all the subviews of the target view
  • setFrame intelligently calls layoutSubviews on the view having it’s frame set only if the size parameter of the frame is different
  • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and it’s superview
  • rotating a device only calls layoutSubview on the parent view (the responding viewControllers primary view)
  • removeFromSuperview – layoutSubviews is called on superview only
Ben Clayton
  • 80,996
  • 26
  • 120
  • 129
skytz
  • 2,201
  • 2
  • 18
  • 23
  • 4
    +1 for the answer. But please consider adding some of the description from the link in your answer over here so that if in future in any case the link goes down, your answer still remains useful. – Parth Bhatt Apr 03 '12 at 07:00
1

You are correct. Layout subviews is automatically invoked. Check the docs for more information on it.

The docs don't specifically say WHEN this is called. However, you can pretty much guarantee it is called rather often. You really only want to override this method when you can't have your subviews resize or move themselves using struts and springs. One instance would be reorganizing subviews positions when the device changes orientation.

Joey J
  • 1,355
  • 10
  • 26
1

The layoutSubviews method will be called any time the system thinks your view needs to be layed out again. For example, if the view's frame is changed, or a subview is added, etc. If you need to manually trigger this, you can call the setNeedsLayout method.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151