Answers for "if dark mode swiftui"

2

dark mode change immediately swift

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
   return
}
appDelegate.changeTheme(themeVal)


// App Delegate Change Theme method
func changeTheme(themeVal: String) {  
  if #available(iOS 13.0, *) {
     switch themeVal {
     case "dark":
         window?.overrideUserInterfaceStyle = .dark
         break
     case "light":
         window?.overrideUserInterfaceStyle = .light
         break
     default:
         window?.overrideUserInterfaceStyle = .unspecified
     }
  }
}
Posted by: Guest on November-27-2019
0

how to check if there is dark mode code wise swiftui

//You can use the colorScheme Environment Key

struct ContentView: View {
	
  @Environemnt(\.colorScheme) var colorScheme
  
  var body: some View {
    Text("Hello")
    .foregroundColor(colorScheme == .light ? .black : .white)
    
    //The tenary operator checks if the view is either in light mode or in dark mode
    //If it is in light mode, the font colour of the text would be black, otherwise it is white
  }
}
Posted by: Guest on October-24-2021

Code answers related to "Swift"

Browse Popular Code Answers by Language