ios - UISegmentedControl iOS 13 清晰配色

标签 ios swift uikit uisegmentedcontrol ios13

在 iOS 12 上,要获得具有清晰边框、清晰分隔线的 UISegmentedControl,一切都很容易。我所做的就是这样:

  settingControl.tintColor = .clear

   let font = myFont
   let boldfont = myBoldFont

  settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font:font], for: .normal)
  settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red, NSAttributedString.Key.font:boldfont], for: .selected)

然后 UISegmentedControl 是完全清晰的颜色(分隔线、背景、边框)

但在 iOS 13 中,我无法完全弄清楚。我可以设置

settingControl.selectedSegmentTintColor = UIColor.clear

但它仍然没有清除背景颜色和分隔线。

我尝试将 backgroundColor 设置为清除,但没有效果。

 settingControl.backgroundColor = UIColor.clear

我也试过设置一个清晰的图像,但仍然没有:

 public extension UIImage {

  /**
   Returns image with size 1x1px of certain color.
   */
 class func imageWithColor(color : UIColor) -> UIImage? {
    let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()

    context?.setFillColor(color.cgColor)

    context?.fill(rect)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image
}

 }

然后是:

  let clearImage = UIImage.imageWithColor(color: UIColor.clear)
    settingControl.setDividerImage(clearImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)


   settingControl.setBackgroundImage(clearImage, for: .normal, barMetrics: .default)
   settingControl.setBackgroundImage(clearImage, for: .selected, barMetrics: .default)

这是它在 iOS 12 上的样子。在 iOS 13 上,这似乎是不可能的。

This is how it looks like on iOS 12. On iOS 13, this seems impossible.

最佳答案

这是一种在 iOS 13 中复制普通分段控件的方法:

import UIKit
import PlaygroundSupport

class PlainSegmentedControl: UISegmentedControl {
    override init(items: [Any]?) {
        super.init(items: items)

        setup()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Used for the unselected labels
    override var tintColor: UIColor! {
        didSet {
            setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
        }
    }

    // Used for the selected label
    override var selectedSegmentTintColor: UIColor? {
        didSet {
            setTitleTextAttributes([.foregroundColor: selectedSegmentTintColor ?? tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
        }
    }

    private func setup() {
        backgroundColor = .clear

        // Use a clear image for the background and the dividers
        let tintColorImage = UIImage(color: .clear, size: CGSize(width: 1, height: 32))
        setBackgroundImage(tintColorImage, for: .normal, barMetrics: .default)
        setDividerImage(tintColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)

        // Set some default label colors
        setTitleTextAttributes([.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
        setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
    }
}

下面是一些放在 playground 中的测试代码:

// Create a dark green view as a test background
let bg = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100))
bg.backgroundColor = UIColor(red: 0.224, green: 0.408, blue: 0.467, alpha: 1)

// The plain segmented control
let seg = PlainSegmentedControl(items: ["Number One", "Number Two", "Number Three"])
seg.tintColor = .white
seg.selectedSegmentTintColor = .green
seg.selectedSegmentIndex = 0
bg.addSubview(seg)
PlaygroundPage.current.liveView = bg

下面是 UIImage 扩展,用于根据颜色创建尺寸图像:

extension UIImage {
    convenience init(color: UIColor, size: CGSize) {
        UIGraphicsBeginImageContextWithOptions(size, false, 1)
        color.set()
        let ctx = UIGraphicsGetCurrentContext()!
        ctx.fill(CGRect(origin: .zero, size: size))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        self.init(data: image.pngData()!)!
    }
}

关于ios - UISegmentedControl iOS 13 清晰配色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57450865/

相关文章:

c# - 如何在 DialogViewController 上将背景设置为透明

ios - Swift 3.0 无法从 UITableView 中删除行

ios - 包含 UIStackViews 的 UITableViewCell 使用大量内存

android - 如何让用户从图库中选择或在 Cordova 中拍照选项

ios - 以编程方式为不同尺寸类别创建具有不同常量的自动布局约束

compiler-errors - 限制表达式中的术语数?

cocoa-touch - 检查 UIView 是否被触摸?

iphone - UITextView 上的变为FirstResponder 不起作用

iphone - 这个 (NSNull*)controller == [NSNull null] 应该做什么?为什么不直接 Controller == nil?

ios - 将结构从 ObjC 传递到 Swift