UIScrollView with subviews shown left and right.

Andy Wong
2 min readDec 4, 2019

--

Recently, I have been trying to figure out how to use a scrollview to display/hint that the view is scrollable by allowing the previous and next views to be vaguely displayed next to the main subviews. After hours and about a day of searching, I was pretty much stuck. There were many great tutorials online. Many solutions were to use a CollectionView or to mess around with contentOffsetin scrollViewDidEndDragging or using hitTest() .

I did not like any of the methods. I wanted to only use a scrollview and nothing else as it was really just a display of cards.

To get started, we are going to have a scrollView. You can put it anywhere on the screen. I liked it in the center so that is where I am placing it.

Next, you want the scrollview to be the frame of subview + padding. So whatever the size of the subview + padding will be the frame of the scrollView. This is done because we will be setting clipsToBound = false so the subviews will go past the scrollview which will give use the desired effect.

Now after adding the scrollview, you want to add subviews to the scrollview. So you can do this in some function. That is up to you. The key here really is that, you want to set the frame of the subview to be the same as the scrollview, subtract the padding on the left and right. So if the subview is 100 in frame, the the scrollView should be 110. Then for the subview’s frame.origin.x it should be the width of the scrollview, minus half of the padding, so half of 10 is 5.

If you run the app now, you should have a scrollView with left and right pages sticking out.

Andy Wong…

A iOS Engineer learning new things all the time.

I also make YouTube videos on iOS! (Channel)

I teach courses on Skillshare (Use my link for a free trial to SkillShare Premium).

Let’s Connect! (Twitter)(Website)(Linkedin)

--

--