setting background image of UISegmentedControl

adit picture adit · May 24, 2012 · Viewed 21.4k times · Source

So I have the following code to set the segmented control background color:

UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 150, 35)];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:@"icon-home.png"] atIndex:0 animated:NO];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:@"icon-star.png"] atIndex:1 animated:NO];

It all works fine except that when I click on the segment I can see the blue button highlighted. How do I disable this highlight? Here's a screenshot of my problem:

enter image description here

Answer

Nandha picture Nandha · May 25, 2012

UISegmentControl has a Divider between two segments. By using the following code you can change the divider background.

[segmentedCtrl setDividerImage:[UIImage imageNamed:@"divider_selected.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setDividerImage:[UIImage imageNamed:@"divider_normal.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Where divider_selected.png is a image which you used for selected state. Where as divider_normal.png is a image which you used for normal state.

Hope this will help....