Getting a ScrollView to work with Autolayout and storyboards

I am trying to make a very simple layout for an app I want to build, but I seem to be struggling with the ScrollView and getting it working via Storyboard. Basically I am trying to build below:

template

I've used constraints using multiple tutorials but it either doesn't scroll or doesn't look right. Any suggestions?

enter image description here

I am getting the following warning:

Warnings

+3


source to share


6 answers


You must provide a contentSize for the UIScrollView when you install it using constraints.

For example:



You can set the width of the view to be equal to the scrollView as this will provide the width of the scrollable content. Then, to provide the height of the content, layout the red and yellow images vertically and use them to provide the height of the view. This means that you will need to specify the starting height for the yellow view, or you can use the view that provides the internal ContentSize attribute to define the yellow view height, so you don't have to manually change it.

Also I think you need to remove the top of the top view topview redview and add redview top == Open top and bottom bottom window bottomview = Open bottom

+3


source


Set a limit on the width and height of the view that is inside the scroll.

If the width is fixed, add constraints so the view width is equal to the Scrollview.

If the height is variable, then set the current height and change its priority to 750.



For example, in the below image, I have set the width and height of the view.

Note that if the height is a variable then the view needs to know its height based on the height of the view content

enter image description here

+1


source


For this UIScrollView

to work perfectly, you need to make sure that the content inside UIScrollView

can output its height at all times . I will consider the child view as the content view UIScrollView

. In doing so, you need to do the following:

  • To yours UIScrollView

    add top, end, top and bottom. And to your content view, add leading, top, trailing and bottom to UIScrollView

    . Xcode should now complain about missing restrictions. Now you need to add an equal height and width constraint to the main view UIViewController

    and give priority to say 250 equal to the height constraint.
  • Now add constraints for top, top and end and fixed heights to your red view.
  • Now add top, end, top and bottom to the yellow view. You can now either programmatically set the height of the yellow image, or add content to it, which allows the yellow view to output its height, which in turn allows the content view to know its height.

What limits the low priority height constraint is to provide enough constraints on the presentation of the content so that Xcode doesn't complain, and since the priority is low it will break if the content inside is too large to be fully displayed on the screen.

Note: Keep your view hierarchy the same as hers.

+1


source


Be aware that a view in a ScrollView must be explicitly sized (layout with a scrollview does not explicitly size it). Usually you can expand the width of the view with root view or fixed value. View Height you also capture a value or layout with views inside it (views inside it must have an explicit height -> view height can be programmed). This is my experience, I hope he can help you :))

0


source


set the height and width of the view, which are inside the scrollView. if view height is fixed then set constraints and for variable height view set height of that view and wire it with viewController as NSLayoutConstrains and change programmatically and also update scrollView height with

scrollView.contenSize.height = fixedViewHeight + variableViewHeight

to change the scroll height.

0


source


Keys for ScrollView to work

  • UIScrollView

    should only use one subview. This UIView

    that serves to browse the content to keep whatever you want to scroll.
  • Make the content view and the parent scroll view have equal width for vertical scrolling (or equal heights for horizontal scrolling). If the ScrollView is inside a complex layout, it is sometimes easier to scroll the ScrollView inside its own parent content. This makes it easier to match the width (or height) of the child to the parent.
  • Make sure all scrolling content is of the specified width and is docked on all sides.

See my complete answer with an example here .

0


source







All Articles