Using a Stack View in a Scroll View and respecting Safe Area insets
I found a way to do this entirely within Interface Builder. Instead of embedding the stack view within the scroll view directly, add a UIView
inside the scroll view to be the content view. Create leading, trailing, top, and bottom constraints all equal to the superview with a constant of 0, plus an equal width constraint to the scroll view. Then embed the stack view inside this content view. Create leading and trailing constraints for the stack view equal to the superview's margin¹, plus top and bottom.
So your setup will be:
- view
-- scroll view
--- view
---- stack view
This will ensure on iPhone X in landscape you can scroll at the left and right edges, the scroll bar is placed at the far right edge of the screen, and the stack view is positioned within the safe area.
¹ I've found a constant of 8 adds enough padding to feel about right.