Razer
Razer

Reputation: 8211

Remove frame of UISegmentedControl

Is there a way in iOS 7 to completely remove the outer frame of an UISegmentedControl? There should be only one rectangle in tint color for the selected segment.

Upvotes: 2

Views: 376

Answers (1)

Jamie McDaniel
Jamie McDaniel

Reputation: 1809

First, create three images and add them your your Image Assets.

  • Blank (a transparent png of any size)
  • Highlight (a png of any color and size)
  • Select (a png of any color and size)

Xcode Image Assets Catalog showing Blank, Highlight, and Select image sets added

Then add this code:

UIImage *backgroundImage = [UIImage imageNamed:@"Blank"];
UIImage *highlightedImage = [UIImage imageNamed:@"Highlight"];
UIImage *selectedImage = [UIImage imageNamed:@"Select"];
UIImage *dividerImage = [[UIImage alloc] init];
[segmentedControl setBackgroundImage:backgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:highlightedImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:selectedImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedControl setDividerImage:dividerImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

UISegmentedControl with clear background and no frame. Selected color is blue. Highlighted color is light blue.

Upvotes: 3

Related Questions