0

Im trying to implement scrollable stackview in my app but I cant get it to work.

screenshot of the screen

When I click on the textfield, an uipicker is shown. Based on the selected row ( 1 to 15 ), an UIButton is added to stackview.

for index in 1...selectedRow+1  {

    let button = UIButton()
    button.setTitle("\(index)", for: .normal)
    directionsStackView.addArrangedSubview(button)

}

This looks pretty good, unless the number is > 7

screenshot of that

So Ive decided to implement UIScrollView to scroll through the stackview

The StackView has the alignment set to Fill and Distribution to Equal Spacing.

Constraints:

ScrollView has: Height Equals to 35, trailing and leading to margins

StackView has: trailing, leading, top, bottom to superview, equal height with superview, and aligned center X to superview ( superview is the scrollview )

The thing I want to accomplish is - the spacing between numbers should always be equal, and when the there are too many numbers ( 7+ ), do not modify the spacing between numbers to be less, just scroll through them horizontally.

I thought that increasing the width of stackview would work, but the problem is that i am not able to scroll through them.

screenshot: htt ps://imgur.com/a/roi38 (sorry for the space im not able to post 3+ links because of reputation)

Can anyone help me with this?

Thank you.

RJq
  • 11
  • 3

2 Answers2

0

StackView is not scrollable by default so, yes, you need a ScrollView.

You may have forgot to set the contentSize of the ScrollView (example on how you could do that)

However, a better solution to your problem would probably have been to use a CollectionView.

Yoam Farges
  • 773
  • 4
  • 16
-1

Select all the buttons and embed them in scrollView instead of stackView in following manner.

enter image description here

amish
  • 335
  • 4
  • 17