首页 > 解决方案 > IOS中分段控制的背景颜色

问题描述

我有一个分段控件,选中时需要为蓝色未选中需要为白色。我在未选择时设置色调颜色时遇到问题,由于某种原因,它总是变成灰色。

这是它的样子:

在此处输入图像描述

蓝色边框实际上是一个带有一些边框颜色的 UIView,我在其中粘贴了分段控件。但我不能改变灰色背景(应该是白色的)。

我试着做所有这些:

    sc.selectedSegmentTintColor = UIColor.blue
    sc.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .body),NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
    sc.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .body),NSAttributedString.Key.foregroundColor: UIColor.blue], for: .normal)
    sc.tintColor = .white
    sc.backgroundColor = .white
    sc.isOpaque =  true

没有成功。:(

标签: colorsbackgrounduisegmentedcontrol

解决方案


它与背景颜色一起工作正常

self.backgroundColor = .red

它在 iOS 13 中运行良好


推荐阅读