Horizontal UIScrollView using auto layout

Viewed 6422

I have been trying this for quite sometime now and went through a lot of posts on SO and some video tutorials. What I want to create is a horizontal scroll view with some buttons in it, like this:enter image description here

My view hierarchy is as follows:

  • View Controller
    • View
      • Scroll View
        • View
          • Buttons

I have set top, leading, trailing, bottom constraints to the scroll view. I have set it's width equal to it's superview, and have set it's height to 200. So far so good, for the view inside the scroll view, I have set it's constraints leading, trailing, top and bottom to zero with respect to it's superview i.e. scroll view. I have made it's width equals to the View controllers view, since that was the solution here on SO to the ambiguous width issue. It solved the issue. Now I added all the buttons and set up their constraints to their parent view. Now when I run the app, a screen like the above added screenshot appears, however I cant scroll to the last element.Any help is greatly appretiated.enter image description here

3 Answers

For swift 5.1

  1. firstly put scrollView give constraint to superView. Make them all what you desire.

  2. Then put view on scroll view like I showed in pic. Give constraint shown below then to avoid error follow 3. step.

  1. Push control button and drag to the view then select Equal Height.
  1. I give name to view over scrollview which name is ContentView . Be sure view size form is freeform and make width 1200 same as ContentView.

5)Then put TableView over the ContentView and give all the constraint zero.

  1. This is my TableviewCell
  1. And this one is the result.

https://i.stack.imgur.com/zxBAT.gif

Related