0

I am trying to create a xib based UIView.

I'd like to add a UIScrollView and then embed my content with in that, so that it scrolls when it does not fit and vice versa.

I have created the xib and added a scroll view, with anchors of 0 on top, left, bottom and right.

I am getting an error that the height and width is ambiguous however.

enter image description here

Historically I have always built these views in code, using something as simple as

...
      scrollView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
      scrollView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
      scrollView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor),
      scrollView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
...
      profilePictureImageView.heightAnchor.constraint(equalToConstant: 170),
      profilePictureImageView.widthAnchor.constraint(equalToConstant: 170),

      collectionView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
      collectionView.topAnchor.constraint(equalTo: profilePictureImageView.bottomAnchor, constant: 6),
      collectionView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
      collectionView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -32),
      collectionView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),

How do I create a full screen scroll view in a xib?

I have a seen a bunch of answers that suggest some random height of 1000, but that seems strange, especially as it does not require that approach in code.

I would like it to be full width and the height of all content inside it.

I did attempt to make xib base a UIScrollView instead but all my content was squashed to the left.

Harry Blue
  • 3,068
  • 5
  • 24
  • 58

2 Answers2

0

try to embed scrollview in a UIView (with the same constraints), or you can use a StackView embedded in a scrollView.

Here a great explanation.

Alessio Campanelli
  • 902
  • 10
  • 18
0

One option to solve your issue is to disable the Content Layout Guides

enter image description here

See also: Scrollable content size ambiguity

πter
  • 1,466
  • 2
  • 6
  • 18