Setting height for UIScrollView

0

I'm creating an application for iOS using Swift , I'm having a lot of difficulty regarding height of UIScrollView , I'm using auto-layout and I create the following structure:

  • UIScrollView
    • UIView // View where I enter the components
      • UIImageView // Constraint Top Edges 20 in relation to UIView
      • UITextView // Constraint Top Edges 40 in relation to UIImageView
      • UITextView // Constraint Top Edges 20 in relation to UITextView
      • UIButton // Constraint Top Edges 30 in relation to UIButton

I'm currently using the following logic to calculate the height for UIScrollView

override func viewDidLayoutSubviews() {
    var scrollHeight : CGFloat = 0.0

    for view in self.containerView.subviews {
        view.layoutIfNeeded()
        scrollHeight += view.frame.size.height
    }

    // Adicionar o tamanho da scrollview, de acordo com a soma
    // das alturas das views filhas
    self.scrollView.contentSize.height = scrollHeight

}

But I can only get the height of the items, without calculating the spaces created by constraints . I would like to know some way to set the height for UIScrollView accurately.

    
asked by anonymous 07.07.2017 / 03:22

1 answer

2

Try:

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