【问题标题】:iOS Swift: UISegmentedControl Tint Colour overlaps with the titleiOS Swift:UISegmentedControl Tint Color 与标题重叠
【发布时间】:2020-06-29 16:27:34
【问题描述】:

我希望所有以前的 iOS 版本都具有 iOS 13 UISegmentedControl 外观。我尝试使用颜色和边框自定义 segmentControl。这是我的代码:

private func create(_ items: [String]) {
        segmentControl.backgroundColor = .lightGray
        segmentControl.cornerRadius = 10
        segmentControl.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.04).cgColor
        segmentControl.borderWidth = 0.5
        items.enumerated().forEach { (index, item) in
            segmentControl.setTitle(item, forSegmentAt: index)
            if let titleAttributes = titleAttributes {
                segmentControl.setTitleTextAttributes([.foregroundColor: titleAttributes.color, .font: titleAttributes.font], for: .normal)
            }
        }
        if #available(iOS 13.0, *) {
            segmentControl.selectedSegmentTintColor = .white
        } else {
            segmentControl.tintColor = .white
        }
    }

上面的代码在iOS13上完美运行,但是对于之前的版本,选择的段tintColor就不合适了。我为选定的片段设置了“白色”,为背景设置了“灰色”。所选的淡色似乎使标题变淡。边框也没有显示。

附件是屏幕截图,所选段色调“白色”不正确。好像标题已经落后了。

而在 iOS 13 中,它看起来很完美

我尝试了很多解决方案,但都不起作用。或者有什么方法可以让 iOS 13 出现在早期版本中?请帮忙。

谢谢!!!

【问题讨论】:

    标签: ios swift uisegmentedcontrol swift5


    【解决方案1】:

    tintColor 更改为selectedColor 您的新代码将是:

    private func create(_ items: [String]) {
            segmentControl.backgroundColor = .lightGray
            segmentControl.cornerRadius = 10
            segmentControl.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.04).cgColor
            segmentControl.borderWidth = 0.5
            items.enumerated().forEach { (index, item) in
                segmentControl.setTitle(item, forSegmentAt: index)
                if let titleAttributes = titleAttributes {
                    segmentControl.setTitleTextAttributes([.foregroundColor: titleAttributes.color, .font: titleAttributes.font], for: .normal)
                }
            }
            if #available(iOS 13.0, *) {
                segmentControl.selectedSegmentTintColor = .white
            } else {
                segmentControl.selectedColor = .white
            }
        }
    

    tint color -> 改变视图的整体颜色 选定的颜色 -> 选择对象时更改!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      相关资源
      最近更新 更多