2

There are lots of viewDidLayoutSubviews events fired in my ViewController. Eventually I narrowed down to the single line of code which triggers the event:

self.debugLabel.text = [NSString stringWithFormat:@"%@ - %@", key, [@(tag) stringValue]];

Its a UILabel in a subview added to self.view of the ViewController. Why changing the text of a UILabel will cause boundary changes? In viewDidLayoutSubviews doc:

When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method.

p.s. I am sure the debugLabel font size is small enough to fit in its frame.

ohho
  • 47,708
  • 70
  • 236
  • 372

1 Answers1

1

I believe this has to do with auto-layout. If a view resizes then the constraints need to be checked throughout the rest of the view hierarchy to ensure that they are satisfied.

Guy Kogus
  • 6,960
  • 1
  • 23
  • 28
  • 1
    To put it more simply, a label's size constraints come by default from its _intrinsic size_ - which changes every time the label's contents change. – matt Nov 13 '14 at 15:55
  • A completely clean project, without `Auto Layout`. Add a single `UILabel` to the view (in Storyboard) and you get a second `viewDidLayoutSubviews` call. Printing the root view and all its subview frames ( `viewDidLayoutSubviews`) shows non has changes on the second call. However, adding 5 more labels does not add any `viewDidLayoutSubviews` calls. Still unclear to me. – bauerMusic Nov 29 '15 at 05:56