Swift development tips and experiences by Tommi Kivimäki

Browse all notes

Custom dynamic colors in iOS

I like dark mode. I also noticed that the colors in Camping Finland were partly broken in iOS dark mode. It was clear that dark mode support had to be the first iOS 13 feature to build.

I updated the app to use system dynamic colors for some backgrounds and gray elements. Then I needed to define some custom dynamic colors. I implemented them as UIColor extensions.

        
          extension UIColor {
            static var skyMiddle: UIColor {
              if #available(iOS 13, *) {
                return UIColor.init { (traitCollection: UITraitCollection) -> UIColor in
                  if traitCollection.userInterfaceStyle == .dark {
                    return UIColor(red: 192/255, green: 36/255, blue: 49/255, alpha: 1)
                  } else {
                    return UIColor(red: 55/255, green: 110/255, blue: 213/255, alpha: 1)
                  }
                }
              } else {
                return UIColor(red: 55/255, green: 110/255, blue: 213/255, alpha: 1)
              }
            }
          }