Set height for Uiscrollview

Asked

Viewed 43 times

0

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

  • Uiscrollview
    • Uiview // View where I insert 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 am currently using the following logic to calculate the height for the 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’d like to know some way to set the time for the UIScrollView accurately.

1 answer

2

Try:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.