ios 设置 UISegmentedControl 的背景图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10740373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:30:08  来源:igfitidea点击:

setting background image of UISegmentedControl

iphoneobjective-ciosipaduisegmentedcontrol

提问by adit

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

在此处输入图片说明

回答by Nandha

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

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

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.

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

Hope this will help....

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

回答by Amy Worrall

Set your same background images for the highlighted state:

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

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