Not able to set color to segment control

0

I'm having color like this "CC0000"

Now to assign this color to my selected segment, I tried using this extension first.

extension UIColor {

    // MARK: - Initialization

    convenience init?(hex: String) {
        var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")

        var rgb: UInt32 = 0

        var r: CGFloat = 0.0
        var g: CGFloat = 0.0
        var b: CGFloat = 0.0
        var a: CGFloat = 1.0

        let length = hexSanitized.count

        guard Scanner(string: hexSanitized).scanHexInt32(&rgb) else { return nil }

        if length == 6 {
            r = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
            g = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
            b = CGFloat(rgb & 0x0000FF) / 255.0

        } else if length == 8 {
            r = CGFloat((rgb & 0xFF000000) >> 24) / 255.0
            g = CGFloat((rgb & 0x00FF0000) >> 16) / 255.0
            b = CGFloat((rgb & 0x0000FF00) >> 8) / 255.0
            a = CGFloat(rgb & 0x000000FF) / 255.0

        } else {
            return nil
        }

        self.init(red: r, green: g, blue: b, alpha: a)
    }

    // MARK: - Computed Properties

    var toHex: String? {
        return toHex()
    }

    // MARK: - From UIColor to String

    func toHex(alpha: Bool = false) -> String? {
        guard let components = cgColor.components, components.count >= 3 else {
            return nil
        }

        let r = Float(components[0])
        let g = Float(components[1])
        let b = Float(components[2])
        var a = Float(1.0)

        if components.count >= 4 {
            a = Float(components[3])
        }

        if alpha {
            return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
        } else {
            return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
        }
    }

}

And I'm using it like so..

let color = UIColor(hex: selectedColor). //"selectedColor" is "CC0000"

But now when I print color, I get Optional(UIExtendedSRGBColorSpace 0.8 0 0 1)

But I'm not sure how I'll set this color as the tintColor of my segmentControl. If I do something like self.segCriticalityABCD.tintColor = color, it is not displaying any color..

ios
swift
hex
uicolor
asked on Stack Overflow May 19, 2020 by user13032222

1 Answer

1

The color created by your extension is working properly:

guard let color = UIColor(hex: "#CC0000") else { return }

This will set the text color of the selected segment:

segmentedControl.setTitleTextAttributes([.foregroundColor: color], for: .selected)

This will set the color of your selected segment:

segmentedControl.selectedSegmentTintColor = color

Other valuable information is available here:

How to set UISegmentedControl Tint Color for individual segment

and here:

https://developer.apple.com/documentation/uikit/uisegmentedcontrol

answered on Stack Overflow May 19, 2020 by SwiftySteve

User contributions licensed under CC BY-SA 3.0