【问题标题】:UISegmentedControl iOS 13 SelectedTint Default ColorUISegmentedControl iOS 13 SelectedTint 默认颜色
【发布时间】:2019-08-26 08:47:27
【问题描述】:

我想将UISegmentControl 中的默认SelectedTintColor 匹配到另一个视图,但我无法从属性检查器中提供的默认系统* 颜色中找到匹配的颜色。

背景似乎是.quaternarySystemFill,但在明暗模式下都没有与所选颜色相匹配的颜色。

即使以这种方式设置颜色也不起作用,因为.selectedSegmentTintColor 返回nil

label.backgroundColor = segmentedControl.selectedSegmentTintColor

有人知道正确的颜色吗?或者我需要使用自定义颜色吗?

编辑:

编辑 2: 目前这条线路可行,但也许有更好的方法?

label.backgroundColor = label.traitCollection.userInterfaceStyle == .light ? .white : .tertiaryLabel

【问题讨论】:

    标签: swift ios13 xcode11


    【解决方案1】:

    根据我对您问题的理解,您想更改与 UISegmentControl 色调相同的背景颜色。你需要做的就是使用这个:

    label.backgroundColor = segmentedControl.tintColor 
    

    希望对你有帮助

    【讨论】:

    • 谢谢! .tintcolor 在情节提要中设置为默认值,但根本不影响视图。例如,如果我将其设置为.systemPink,则所选索引仍然是白色/灰色。我在我的问题中添加了两张图片。
    • 好的,我现在遇到了这个问题,您只需将其背景颜色从情节提要中清除即可。这是由于默认背景选择和默认选择根据主题而发生的。将 segmentControl 背景颜色设置为清除后,您将获得所需的结果。谢谢它,这有助于投票。
    • 不幸的是,这也没有改变任何东西。也许这在以前的 iOS 版本中确实有效,但它们在 iOS13 中确实改变了一些东西。如果我在属性检查器中更改selectedSegmentTintColor ,它确实会正确更改所选索引的颜色,但如果我将其保留为默认值,如果我尝试通过 swift 以编程方式访问它,它会返回nil
    • 尝试在viewDidLoad中设置你的UISegmentcontrol背景颜色清除,你会得到你的结果。我已经尝试过了,它对我有用。 segment.backgroundColor = Color.clear希望这会有所帮助
    【解决方案2】:

    如果你想改变与段条色调相同的背景,你可以尝试:

    yourLabel.backgroundColor  = segmentControl.tintColor.
    

    我试过了

    var myNormalFont = UIFont.FromName("OpenSans-SemiBold", 19f); var myNormalTextColor = UIColor.LightGray; AlertsSegmentedControl.SetTitleTextAttributes(new UITextAttributes() { Font = myNormalFont, TextColor = myNormalTextColor }, UIControlState.Normal); var mySelectedFont = UIFont.FromName("OpenSans-SemiBold", 19f); var mySelectedTextColor = UIColor.White; AlertsSegmentedControl.SetTitleTextAttributes(new UITextAttributes() { Font = mySelectedFont, TextColor = mySelectedTextColor }, UIControlState.Selected); AlertsSegmentedControl.BackgroundColor = UIColor.Clear;

    【讨论】:

    • 感谢您的回答,但这也不起作用,如上一个答案的 cmets 中所述。
    • 您是否将段控件的背景设置为 .clear?
    猜你喜欢
    • 2019-12-18
    • 2019-10-19
    • 2017-07-28
    • 2020-01-26
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多