Search code examples
iosswiftxcodeuiviewuisegmentedcontrol

Swipeable UISegmentedControl


I need to implement a Swipeable UISegmentedControl in Swift. I have UISegmenttedControl view, but how can I make in swipeable like a tab bar in Android.

I have a git MIT licensed project HMSegmentedControl, but I am required to implement it standalone. I need a a simple guidance.


Solution

  • Use UISegmentControl as Subview of UIScrollView

    I'm using UIStoryboard to design my View. Please follow these steps.

    Step 1: Add UIScrollView to your storyboard.

    Step 2: The width of the UIScrollView should be of View's width, height as approx 60px.

    Step 3: Add UISegmentControl with n Segments to the UIScrollView, the width of the UISegmentControl should be of 100*n px (You can calculate based on your requirements).

    Step 4: Now, Add Outlet for UIScrollView in your ViewController.

    Step 5: Add contentsize for the UIScrollView, following code in the viewDidLoad of your ViewController

    self.scrollView.contentSize = CGSizeMake(100*n, 60);
    

    Please do your own customizations for UISegmentControl. I have tested the same & it is working for me.

    This should work for you!