Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting background image of UISegmentedControl

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

like image 694
adit Avatar asked May 24 '12 15:05

adit


2 Answers

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....

like image 191
Nandha Avatar answered Nov 03 '22 09:11

Nandha


Set your same background images for the highlighted state:

[segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
like image 27
Amy Worrall Avatar answered Nov 03 '22 09:11

Amy Worrall