Why are the colors I assign to UIControls not the colors that show up?

0

Since iOS 13, the colors getting assigned to UIControls don't seem to be the same colors that ultimately end up getting displayed.

Here is an example that can be copy & pasted into an xcode playground:

import UIKit
import PlaygroundSupport

// https://www.hackingwithswift.com/example-code/uicolor/how-to-convert-a-hex-color-to-a-uicolor
extension UIColor {
    public convenience init?(hex hexUser: String) {
        let r, g, b, a: CGFloat

        let hex = "\(hexUser)FF" // append alpha compnent for ease of example

        if hex.hasPrefix("#") {
            let start = hex.index(hex.startIndex, offsetBy: 1)
            let hexColor = String(hex[start...])

            if hexColor.count == 8 {
                let scanner = Scanner(string: hexColor)
                var hexNumber: UInt64 = 0

                if scanner.scanHexInt64(&hexNumber) {
                    r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
                    g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
                    b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
                    a = CGFloat(hexNumber & 0x000000ff) / 255

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

        return nil
    }
}


let backgroundColorHex = "#303b46" // shows up as #38424c in iOS >= 13
let tintColorHex = "#ebdb34" // shows up as #eadb33 in iOS >= 13

let backgroundColor = UIColor(hex: backgroundColorHex)!
let tintColor = UIColor(hex: tintColorHex)!

class MyViewController: UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let segmentedControl = UISegmentedControl(items: ["One", "Two"])
        segmentedControl.selectedSegmentTintColor = tintColor
        segmentedControl.backgroundColor = backgroundColor
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
        segmentedControl.selectedSegmentIndex = 0
        view.addSubview(segmentedControl)

        segmentedControl.translatesAutoresizingMaskIntoConstraints = false
        segmentedControl.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        segmentedControl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        segmentedControl.widthAnchor.constraint(equalToConstant: 200).isActive = true
        self.view = view
    }
}

PlaygroundPage.current.liveView = MyViewController()

For this UISegmentedControl I am setting a background color of "#303b46", but upon inspection I see iOS renders "#38424c". For the selectedSegmentTintColor I am setting "#ebdb34" but iOS renders "#eadb33".

I am setting a background color of "#303b46", but upon inspection I see iOS renders "#38424c".

 I am setting "#ebdb34" but iOS renders "#eadb33".

I have scoured the Apple docs for answers but have found nothing. They seem to treat tintColor and how its handled very ambiguously. I've looked into UITraitCollection and color profiles, and have not been able to figure this one out.

Does anyone know what mechanisms are in play here for my assigned color to not actually get displayed as assigned? Along with the best way to deal with these inconsistencies when working with design? Thank you.

ios
uikit
ios13
uicolor
uicontrol
asked on Stack Overflow Apr 28, 2020 by Gagan Singh • edited Apr 28, 2020 by Gagan Singh

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0