[Unity 5] How to force a UI layout to fix itself?

I have a canvas with some nested layouts on it. When I SetActive(false) the canvas GameObject, then SetActive(true) it again, the layouts get completely messed up. Everything on the screen is in the wrong place.

I have tried

void OnEnable()
{
  LayoutRebuilder.MarkLayoutForRebuild((RectTransform)transform);
  foreach (RectTransform rect in 
    gameObject.GetComponentsInChildren<RectTransform>())
      LayoutRebuilder.MarkLayoutForRebuild(rect);
}

I’ve even tried putting that in Update() to no effect.

When I resize the window, the layouts fix themselves.

Again note that I am not doing anything at runtime to cause this problem other than disabling and re-enabling the (top level) canvas.

void OnEnable() {
HorizontalLayoutGroup hlg = gameObject.GetComponent();
Canvas.ForceUpdateCanvases();
hlg.CalculateLayoutInputHorizontal();
hlg.CalculateLayoutInputVertical();
hlg.SetLayoutHorizontal();
hlg.SetLayoutVertical();
}

try this

I was using nested canvases. Once I replaced them with panels, so that the only canvas was the outermost screen-space canvas, that fixed the problem.

(I also reduced my usage of layouts by about 50% but didn’t get rid of them completely, but I doubt that was the issue.)