rykeeboy
rykeeboy

Reputation: 675

Issues with named colors on iOS 11 with Xcode 11 beta

I am trying to implement dark mode in my iOS app. I need to use named colors in order to do this, but I've run into a bit of a snag. Everything works fine on the iOS 13 simulator when switching between light and dark mode, and the colors are also correct in the iOS 12 simulator. The problem occurs when I try to run my app on an iOS 11 simulator. Any named colors that I've used in the storyboard default to the dark version of the color, and when I try to access a named color in code I get nil. Just wondering if anyone else has run into this

Upvotes: 4

Views: 4681

Answers (5)

marko nazmy
marko nazmy

Reputation: 21

I highly recommend adding all your colors in separate assets and any added color can be mapped to the new system colors so it will handle light mode and dark mode colors automatically for iOS 13 and it will take the default color for iOS 12,11,... Here are the custom colors

Here how to select one of them in your storyboard

also, you can call it in your code using

UIColor(named: "systemBlue") ?? UIColor.blue

Upvotes: 1

JBarros35
JBarros35

Reputation: 1026

Update Xcode for Version 11.0 (11A420a)

For the ones with this problem and on Storyboards, I recommend on your color-scheme to use the "Any Appearance" default to Light mode. For example, for dark I have gray background and light its white, the fonts are the opposite, white and darkGray. Default everything to light mode, the IOS will interpret it as light and you won't have issues.

enter image description here

As you can see on my screenshots, my app is working properly on IOS 11. **enter image description here** If I don't do that, probably all my fonts will be white and user won't see anything at all.

Upvotes: 1

Jonny
Jonny

Reputation: 16298

It seems that Apple know of it.

Quote from https://developer.apple.com/ios/submit/

Please note that apps built with Xcode 11 using named colors may experience lookup failures (with no value returned) when the app runs on iOS 11 or earlier. This will be fixed in a future Xcode update. To avoid this issue, raise the minimum deployment target to iOS 12 or later to submit to the App Store now, or rebuild with the next Xcode GM candidate seed when it’s available.

Update 20190917

Build your apps using Xcode 11 GM seed 2, which includes SDKs for iOS 13, iPadOS, watchOS 6, tvOS 12, and macOS Catalina. Starting April, 2020, all iOS apps submitted to the App Store will need to be built with the iOS 13 SDK or later. They must also support the all-screen design of iPhone XS Max or the 12.9-inch iPad Pro (3rd generation), or later.

Upvotes: 2

Bilal
Bilal

Reputation: 19156

It's bug in Xcode 11 with IOS 11. Two issues with named colors in IOS 11

  1. UIColor init method init?(named name: String) returns nil in IOS 11
  2. Named color assets used in storyboard or xib files defaults to dark version some times.

Here are workaround for time being until it's fixed in upcoming Xcode release.

  1. If UIColor init method init?(named name: String) returns nil you need to provide fallback color for light mode.

    let color = UIColor(named: "myColor") ?? UIColor.black // default color for IOS 11
    
  2. Named color issue for Story board and xib files, if you observe the changes in your xib file or storyboard after setting any name color you will notice there is a namedColor xml tag under resources tab. And for each namedColor there is fallback color there, that fallback color is being in IOS 11 case because named color isn't working. You can even see the warings in your console.

     <resources>
         <namedColor name="NavigationBar">
             <color red="0.2669999897480011" green="0.70999997854232788" blue="0.046999998390674591" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
         </namedColor>
     </resources>
    

    Bug in Xcode 11 is that default value is set to dark mode version if your MacOS appearance is set to Dark and it picks light color version for default values if your MacOS appearance is set to Light. Workarounds to fix this for IOS 11.

    1. You can set your MacOS appearance to Light. After that you need to open each and every storyboards or xib files in yours project once. Once you open it, editor will automatically pick the Light version of your colors and you can see in your source control that file is modified and default value will be Light version of your color asset. Note that every time your change any value in your color assets, you have to do this again for all the xib files using that named color.
    2. Second is a bit complicated that your write a script that parse all the xib and storyboard files for your project and update the default RGBA values.

Upvotes: 5

Bilal
Bilal

Reputation: 267

Seems like problem is on Apple side. Even Xcode 11 GM seed also having this issue. People are discussing this issue on apple developer forums.

https://forums.developer.apple.com/thread/122053

Upvotes: 1

Related Questions