如何在 iOS 13 的 UISegmentedControl 中更改段的颜色?

How to change the colors of a segment in a UISegmentedControl in iOS 13?(如何在 iOS 13 的 UISegmentedControl 中更改段的颜色?)

本文介绍了如何在 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 中更改段的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 iOS 13 的 UISegmentedControl 中更改段的颜色?