简体   繁体   English

如何在UISegmentedControl中设置禁用段的背景图像

[英]How to set background image for disabled segment in UISegmentedControl

I've tried to set color to disabled segment in UISegmentedControl. 我试图在UISegmentedControl中将颜色设置为禁用的段。 But not yet succeeded. 但尚未成功。 I want to know that if it's possible to set background image for disabled segment in UISegmentedControl. 我想知道是否可以在UISegmentedControl中为禁用的段设置背景图像。

I've tried the following code but it's not working : 我已经尝试了以下代码,但无法正常工作:

NSDictionary *attrs = @{ UITextAttributeTextColor : [UIColor lightGrayColor] };
[self.controlStatus setTitleTextAttributes:attrs forState:UIControlStateDisabled];

[self.controlStatus setBackgroundImage:[[UIImage imageNamed:@"img.png"] retain] forState:UIControlStateDisabled barMetrics:nil];

1st two lines are working. 第一条两条线都在工作。 I'm able to set the color but not the background image or background color when disabled. 禁用时,我可以设置颜色,但不能设置背景图像或背景颜色。

Is there any way to do this ?? 有什么办法做到这一点?

UPDATE: You will have to do it the following way. 更新:您将必须通过以下方式进行操作。

[[UISegmentedControl appearance] setBackgroundImage:[UIImage imageNamed:@"SegmentViewDisabled"]
                                    forState:UIControlStateDisabled
                                    barMetrics:UIBarMetricsDefault];

Looks like you will encounter a bug from Apple's end if you set the divider image for disabled state though, found here 如果您将分隔器图像设置为禁用状态,则看起来您将遇到Apple的错误,请参见此处

-------------------The below does not work-------------------- -------------------下面不起作用--------------------

You can do it this way. 您可以这样进行。 Where SegmentViewDisabled is an image asset in your image assets catalog. 其中SegmentViewDisabled是图像资产目录中的图像资产。

[self.controlStatus setBackgroundImage:[UIImage imageNamed:@"SegmentViewDisabled"] forState:UIControlStateDisable barMetrics:UIBarMetricsDefault];

UISegmentControl has a Divider between two segments. UISegmentControl在两个段之间有一个分隔线。 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];

If you need to customize the appearance of your segmented control beyond standard tinting, you might consider doing so using custom images. 如果您需要自定义分段控件的外观,而不是标准着色,则可以考虑使用自定义图像。 Since segmented controls have different metrics for portrait and landscape device orientations, remember to specify an appropriate image for each set of metrics. 由于分段控件在纵向和横向设备方向上具有不同的度量标准,因此请记住为每组度量标准指定适当的图像。

You can set a background image for each control state of your segmented control using the backgroundImageForState:barMetrics: method. 您可以使用backgroundImageForState:barMetrics:方法为分段控件的每个控件状态设置背景图像。 You should also specify divider images for each combination of left and right segment states to give selected or highlighted segments a different look than segments in a normal state, as shown here: 您还应该为左右段状态的每种组合指定分隔线图像,以使选定或突出显示的段与正常状态下的段具有不同的外观,如下所示:

image1,image2,image3 are different images image1,image2,image3是不同的图像

    [mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal  rightSegmentState:UIControlStateNormal barMetrics:barMetrics];

    [mySegmentedControl setDividerImage:image2 forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:barMetrics];

    [mySegmentedControl setDividerImage:image3 forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:barMetrics];

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM