Implement Android-Like Sliding Tabs in Swift XCode

Anto D.
3 min readAug 5, 2019

Hi.. Welcome back.

This is my first time writing medium story using English :), and maybe in the future I’ll only using English in my medium stories. (I am still learning).

In this story I’ll try to share about implementing android-like sliding tabs in swift xcode.

By default swift not provide sliding tabs in UIKit, so yesterday I’m tried to create a simple library to help me creating this stuff.

First I’m created a class named UISimpleSlidingTabController that extends UIViewController, then created 2 UICollectionView for tab header and content. And then setting up the UICollectionView like register the cell, create the ui cell for header, enabled paging for content, setup deletegate, datasource, flow-layout, scroll direction, handling when the content is swipe and etc.

Then I’m created some function for adding view controllers to content, set current position, set header title, set header background color, set header active color, and set header inactive color.

I’m also created 2 style for the header. namely fixed and flexible, fixed is used if the desired tab is not large and flexible is used if the desired tab is large.

And finally the codes became like this.

TIME TO TEST IT.

Now I’ll try to use it in new view controller. But I’ll try to styling the ViewController first, to look like in the thumbnail.

And now I’ll try to extends the ViewController class to UISimpleSlidingTabController, adding some item, setting up the header color, and tab style.

For flexible style just add this codes before build()

CONCLUSION:

Using UICollectionViewController for creating sliding tabs is quite fun and easy, because the UICollectionViewFlowLayout make it easier to change the direction of scrolling, paging and etc.

The script uploaded in github: https://github.com/erthru/SlidingTabsExample

Okay, See you again in another story. :)

--

--