为iOS 13中的每个子视图设置UISegment文本颜色

问题描述 投票:0回答:1

我正在更新我的应用程序,但遇到一个问题,我无法弄清楚如何为每个子视图设置UISegmentedControl的默认文本颜色。我不想将两者设置为相同的颜色,因此仅使用selectedSegmentTintColor不起作用。这是我在iOS 12及更高版本上的功能

 // segment controls changed in iOS 13
    if #available(iOS 13.0, *) {
       // I can change the text color like this, but this affects both buttons, and I want each to have a different color when not selected
       typeOfStatement.setTitleTextAttributes([.foregroundColor: Styles.negativeColor()()], for: .normal)
    }
    else{
        // change color of segment control
        typeOfStatement.subviews[0].tintColor = Styles.positiveColor()
        typeOfStatement.subviews[1].tintColor = Styles.negativeColor()

    }

[旧方法会给我颜色边框和文本,我知道边框在iOS 13中消失了,但是如果可能的话,我想设置文本颜色。

我已经尝试过类似segment.selectedSegmentTintColor = Styles.positiveColor()的操作,但这为两者都设置了颜色。

该子视图似乎无法访问selectedSegmentTintColor。

我只想为每个文本设置颜色,以便在不选择它们时显示文本颜色。

swift xcode uisegmentedcontrol
1个回答
0
投票

尝试一下:

    let titleTextAttributes = [NSAttributedString.Key.foregroundColor: 
    UIColor.green]
    segment?.setTitleTextAttributes(titleTextAttributes, for: .selected)
    segment?.setTitleTextAttributes(titleTextAttributes, for: .normal)
© www.soinside.com 2019 - 2024. All rights reserved.