setNeedsLayout() of UIView tells the system that you want the current view and all its subviews to be redrawn in the next update cycle.
Whereas layoutIfNeeded() is a synchronous call which tells the system that you want the current view and all its subviews to be redrawn immediately without waiting for the next update cycle.
UIView have one more method named layoutSubviews() which you can use to calculate and set the frame of your view directly, in case auto-layout is not working for you as expected.
Thanks for reading.