skip to Main Content

On xcode I work within a workspace where there is a basic project and a framework. All colors are defined within the framework. When I configure a label with the color assets of the framework from the basic project on storyboard, the color is taken correctly, but if I then switch to dark mode, the color does not change, it always remains the same.

Configuration color label in project

setting textColorBlack in dark mode should appear white but it doesn’t work.

textColorBlack in framework project

I don’t understand what the problem is.

2

Answers


  1. Xib/Storyboard just preserves selected color name (visible at dev time from all spaces), but in run-time on color schema changes it cannot find it, because by default it searches by name in main bundle.

    Instead we have to assign color programmatically specifying exact location of color, like

    override func viewDidLoad() {
        super.viewDidLoad()
    
        label.textColor = UIColor(named: "myColor", 
              in: Bundle(identifier: "framework_identifier") ?? .main,  // << here !!
              compatibleWith: .current)
    }
    
    @IBOutlet weak var label: UILabel!
    

    Tested with Xcode 13.4 / iOS 15.5

    demo

    Login or Signup to reply.
  2. In our case we defined some extensions, that load the corresponding Colors from the asset catalog:

    public extension Color {
        public enum Primary {
            static let green = Color("PrimaryGreen", bundle: .module)
        }
    }
    public extension UIColor {
        public enum Primary {
            static let green = UIColor(Color.Primary.green)
        }
    }
    

    SwiftUI’s Color is loaded directly from the asset catalog, but UIKit’s UIColor is instantiated from the loaded Color. This causes the described problem, since the UIColor won’t automatically update, when switching to dark mode. The solution, of course, is to also load the UIColor directly from the asset catalog .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search