如何在iOS 13中的UISegmentedControl中更改细分的颜色? [英] How to change the colors of a segment in a UISegmentedControl in iOS 13?

查看:609
本文介绍了如何在iOS 13中的UISegmentedControl中更改细分的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UISegmentedControl在iOS 13中有了新外观,并且现有的更改分段控件颜色的代码不再像以前那样起作用.

A UISegmentedControl has a new appearance in iOS 13 and existing code to alter the colors of the segmented control no longer work as they did.

在iOS 13之前,您可以设置tintColor,该键将用于分段控件周围的边框,分段之间的线条以及所选分段的背景颜色.然后,您可以使用titleTextAttributes的前景色属性更改每个句段标题的颜色.

Prior to iOS 13 you could set the tintColor and that would be used for the border around the segmented control, the lines between the segments, and the background color of the selected segment. Then you could change the color of the titles of each segment using the foreground color attribute with titleTextAttributes.

在iOS 13下,tintColor不执行任何操作.您可以设置分段控件的backgroundColor来更改分段控件的整体颜色.但是我找不到任何方法来更改用作选定段背景的颜色.设置文本属性仍然有效.我什至尝试设置标题的背景色,但这只会影响标题的背景,而不会影响所选片段的其余背景色.

Under iOS 13, the tintColor does nothing. You can set the segmented control's backgroundColor to change the overall color of the segmented control. But I can't find any way to alter the color used as the background of the selected segment. Setting the text attributes still works. I even tried setting the background color of the title but that only affects the background of the title, not the rest of the selected segment's background color.

简而言之,您如何在iOS 13中修改UISegmentedControl当前选定段的背景颜色?是否有使用公共API的适当解决方案,而无需深入研究私有子视图结构?

In short, how do you modify the background color of the currently selected segment of a UISegmentedControl in iOS 13? Is there a proper solution, using public APIs, that doesn't require digging into the private subview structure?

iOS 13中没有针对UISegmentedControlUIControl的新属性,并且UIView中的所有更改均不相关.

There are no new properties in iOS 13 for UISegmentedControl or UIControl and none of the changes in UIView are relevant.

推荐答案

从iOS 13b3开始,UISegmentedControl上现在有一个selectedSegmentTintColor.

As of iOS 13b3, there is now a selectedSegmentTintColor on UISegmentedControl.

要更改分段控件的整体颜色,请使用其backgroundColor.

To change the overall color of the segmented control use its backgroundColor.

要更改所选段的颜色,请使用selectedSegmentTintColor.

To change the color of the selected segment use selectedSegmentTintColor.

要更改未选择的段标题的颜色/字体,请使用状态为.normal/UIControlStateNormalsetTitleTextAttributes.

To change the color/font of the unselected segment titles, use setTitleTextAttributes with a state of .normal/UIControlStateNormal.

要更改所选段标题的颜色/字体,请使用状态为.selected/UIControlStateSelectedsetTitleTextAttributes.

To change the color/font of the selected segment titles, use setTitleTextAttributes with a state of .selected/UIControlStateSelected.

如果使用图像创建分段控件,则将图像创建为模板图像时,将使用分段控件的tintColor为图像着色.但这有一个问题.如果将tintColor设置为与selectedSegmentTintColor相同的颜色,则图像在所选片段中将不可见.如果将tintColor设置为与backgroundColor相同的颜色,则未选中段上的图像将不可见.这意味着您的带有图像的分段控件必须使用3种不同的颜色才能使所有内容可见.或者,您可以使用非模板图像而不设置tintColor.

If you create a segmented control with images, if the images are created as template images, then the segmented control's tintColor will be used to color the images. But this has a problem. If you set the tintColor to the same color as selectedSegmentTintColor then the image won't be visible in the selected segment. If you set the tintColor to the same color as backgroundColor, then the images on the unselected segments won't be visible. This means your segmented control with images must use 3 different colors for everything to be visible. Or you can use non-template images and not set the tintColor.

在iOS 12或更早版本下,只需设置分段控件的tintColor或依靠应用程序的整体色调即可.

Under iOS 12 or earlier, simply set the segmented control's tintColor or rely on the app's overall tint color.

这篇关于如何在iOS 13中的UISegmentedControl中更改细分的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆