Shah
Shah

Reputation: 5020

Custom UISegmented Control

How can I acheive this in iOS UISegmentedControl (Objective-C)

I want to show rounded selection for selected segment control.

enter image description here

Any Help would be really appreciated.

Upvotes: 0

Views: 191

Answers (1)

Vivek Molkar
Vivek Molkar

Reputation: 3960

UISegmentedControl has a section for customization.

Summary as follows: All you have to do is provide divider images for different state combinations. That means you need to provide 3 images in all for following states:

  1. Image between two unselected segments
  2. Image between segment selected on the left and unselected on the right
  3. Image between segment selected on the right and unselected on the left

Code as follows:

Objective-C

// Image between two unselected segments.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
                  rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
// Image between segment selected on the left and unselected on the right.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateSelected
                  rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
// Image between segment selected on the right and unselected on the right.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
                  rightSegmentState:UIControlStateSelected barMetrics:barMetrics];

Swift

    // Image between two unselected segments.
    mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
                                       rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)

    // Image between segment selected on the left and unselected on the right.
    mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Selected,
                                       rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)

    // Image between segment selected on the right and unselected on the left.
    mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
                                       rightSegmentState: UIControlState.Selected, barMetrics: UIBarMetrics.Default)

Here is a post that explains how to achieve this.

If you see the linked post those are 3 images. Attaching the screenshot for reference: enter image description here

Upvotes: 1

Related Questions