Johan Nordberg
Johan Nordberg

Reputation: 3777

Full width subviews of uiscrollview with auto layout in iOS

I'm having a problem with making subviews of a UIScrollView be the same with as the root view. The UIScrollView has constraint similar to H:|-0-[scrollview]-0-| and V:|-0-[scrollview]-0-|. All the subviews has horizontal constraints similar to H:|-0-[view]-0-|.

Still, unless I explicitly set a width of at least one subview, the scrollview and all subviews resizes to the smallast possible width of the subviews (in this case with intrinsic content size of the UIButton). It does this even if I explicitly set a width of the UIScrollView itself.

I would like to avoid setting explicit widths, since this UI is shared between both iPhone (4, 5, 6) and iPad.

How can I make the horisontal contentSize of the UIScrollView be the same with as the parent root view and still have the layout work in IB?

Example of messed up layout

Upvotes: 4

Views: 568

Answers (1)

Johan Nordberg
Johan Nordberg

Reputation: 3777

After some more years of iOS development and auto layout, I found that fixing this is quite easy. Just set and equal width constraint between the collection view and the root view, ie the superview of the scrollview.

Upvotes: 1

Related Questions