设置UISegmentedControl的背景图像

时间:2012-05-24 15:16:00

标签: iphone objective-c ios ipad uisegmentedcontrol

所以我有以下代码来设置分段控件背景颜色:

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];

一切正常,但是当我点击片段时,我可以看到突出显示的蓝色按钮。如何禁用此突出显示?这是我的问题的截图:

enter image description here

2 个答案:

答案 0 :(得分:15)

UISegmentControl在两个段之间有一个分隔符。通过使用以下代码,您可以更改分隔符背景。

[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];

其中divider_selected.png是您用于选定状态的图像。 divider_normal.png是您用于正常状态的图像。

希望这会有所帮助....

答案 1 :(得分:2)

为突出显示的状态设置相同的背景图像:

[segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];