0

I'm working on app and decided to use UIScrollView. I'm using Cartography to set the elements constraints inside of the UIScrollView.

My question is: On Cartography, we can get bottom position of element with element.bottom, is there a way to use this position to set the UIScrollView contentSize height? I know what is the last one element.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Victor Camargo
  • 374
  • 9
  • 16

2 Answers2

1

When you use AutoLayout, the contentSize of a scrollView will computes itself. Ie: it will have a height corresponding to the total height of the elements it contains. You should just bind the bottom of your bottom element to the bottom of your scrollView.

| - [view1 (100)] - [view2 (150)] - [view3 (100)] - |

Let's say you have this, with each - representing a horizontal spacing constraint of 0, and the | the borders of the scrollView, thus, the contentSize of the scrollView will be computed to match the total width of the elements it contains and be 350.

You can check this part of the Apple doc on how to work with scrollViews and AutoLayout : https://developer.apple.com/library/content/technotes/tn2154/_index.html

Just remember that you should not set the contentSize of a scrollView manually when using AutoLayout.

Sonastra
  • 182
  • 1
  • 9
0

According to this answer on the Github Cartography project, this is not poissible.

Not sure what you mean, but view.bottom is not a value but rather a variable symbolic (after all, it's Auto Layout's job to figure out the value).

Now what most strikes me is that if I set the constraints manually, without using Cartography, this works correctly.

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0(\(getScreenSize().width * 0.68))]-25-[v1][v2(\(textViewHeight))][v3(44)]-20-[v4(32)][v5(300)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imageView, "v1": titleTextView, "v2": textView, "v3": stackView, "v4": galleryHeader, "v5": collectionView]))
Victor Camargo
  • 374
  • 9
  • 16