How do I auto size a UIScrollView to fit its content How do I auto size a UIScrollView to fit its content objective-c objective-c

How do I auto size a UIScrollView to fit its content


The best method I've ever come across to update the content size of a UIScrollView based on its contained subviews:

Objective-C

CGRect contentRect = CGRectZero;for (UIView *view in self.scrollView.subviews) {    contentRect = CGRectUnion(contentRect, view.frame);}self.scrollView.contentSize = contentRect.size;

Swift

let contentRect: CGRect = scrollView.subviews.reduce(into: .zero) { rect, view in    rect = rect.union(view.frame)}scrollView.contentSize = contentRect.size


UIScrollView doesn't know the height of its content automatically. You must calculate the height and width for yourself

Do it with something like

CGFloat scrollViewHeight = 0.0f;for (UIView* view in scrollView.subviews){   scrollViewHeight += view.frame.size.height;}[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];

But this only work if the views are one below the other. If you have a view next to each other you only have to add the height of one if you don't want to set the content of the scroller larger than it really is.


Solution if you're using auto layout:

  • Set translatesAutoresizingMaskIntoConstraints to NO on all views involved.

  • Position and size your scroll view with constraints external to the scroll view.

  • Use constraints to lay out the subviews within the scroll view, being sure that the constraints tie to all four edges of the scroll view and do not rely on the scroll view to get their size.

Source:https://developer.apple.com/library/ios/technotes/tn2154/_index.html