3

Using Swift5.3.2, iOS14.4.1, Xcode12.4,

I try to make a custom-Color change when switching from light to dark mode in SwiftUI.

Here is my code:

import SwiftUI

struct MyTestView : View {
    
    var body: some View{
        
        VStack {
            Rectangle()
                .foregroundColor(Color("loginBG"))
        }
    }
}

The MyTestView is called right at the beginning of my App lifecycle (real simple example):

import SwiftUI

@main
struct myApp: App {
        
    var body: some Scene {
        WindowGroup {
            MyTestView()
        }
    }
}

Here is my Color definition of loginBG:

enter image description here

Here is the video showing that the Rectangle does unfortunately not change color - why ?????

(from the status-Bar color-switch you can see that I indeed changed from light-mode to dark-mode and back. But no Rectangle-Color change at all)

enter image description here

iKK
  • 6,394
  • 10
  • 58
  • 131

1 Answers1

1

You can try with this code:

import SwiftUI

struct MyTestView : View {
    
    var body: some View{
        
        VStack {
            Rectangle()
                .foregroundColor(Color(UIColor(named: "loginBG")))
        }
    }
}

replace the Color("loginBG") to Color(UIColor(named: "loginBG"))

This can work for me with real device.

onmyway133
  • 45,645
  • 31
  • 257
  • 263
Lisen
  • 11
  • 1