2

I am trying to understand why the same function causes a memory leak if I call it from viewDidAppear rather than viewWillLayoutSubiews.

I have two extensions that fits the sublayers of a view to its bounds:

extension CALayer {
    func fit(rect: CGRect) {
      frame = rect

      sublayers?.forEach { $0.fit(rect: rect) }
    }
}

extension UIView {
  func fitLayers() {
    layer.fit(rect: bounds)
  }
}

And the same call priorityButton.fitLayers() from viewDidAppear doesn't deallocate the Layer objects. Both do the job as expected and works well but I'd like to understand why is it.

Thanks!

juanjovn
  • 492
  • 4
  • 8
  • I guess it's related to `UIViewController` lifecycle, when You call this function in `viewWillLayoutSubiews` it's OK because view will layout, but when called from `viewDidAppear`, all `UIViewController` layout methods called again because your function is layout stuff. `UIViewController` lifecycle explained here: https://stackoverflow.com/a/32688124/5102722 – zzmasoud Apr 19 '20 at 13:08

0 Answers0