Search code examples
swiftxcodeuistackviewviewwillappearviewdidappear

What methods are called when stack view automatically updates


When a UIStackView recalculates its fill proportionally distribution of UI elements upon a change in Stack view size, I'd like to make adjustments to those elements right before they appear. It seems as though there must be a method called between the ViewWillAppear and ViewDidAppear.

Any Idea what happens between these two when the stack recalculates?


Solution

  • viewWillAppear and viewDidAppear have to do with the view controller life cycle and coming on and off screen, but not with layout. You want either UIViewcontroller's viewDidLayoutSubviews or UIView's layoutSubviews method. When these methods are called, auto layout is done with he view controller's subviews or with the UIView's frame, respectively. You can tweak the frames and update things like sublayers that are not affected by auto layout at this point.