SwiftUI Color doesn't update on main page - swift

All my text in my app use the color Color.textColor which changes due to a UserDefault setting:
extension Color {
public static var textColor: Color {
let color = UserDefaults.standard.bool(forKey: "redText") ? Color.red : Color.white
return color
}
}
This is changed via a toggle in my settings section. The problem is, when I toggle this it successfully changes all text colours all my views, apart from the main view.
I've made a gif here to showcase the problem: https://imgur.com/a/LcN7TBi
The bug is there in both simulation and preview.
Edit: When I switch off/on the preview the main page colours actually update to the correct colour.

Related

TabBarItem how to change icon color

The default color of inactive TabBarItem icon is gray
How can I change the default color?
XCode 14.2
SwiftUI 4.x.x
You can set the color with appearance settings:
struct ContentView: View {
init() {
UITabBar.appearance().unselectedItemTintColor = UIColor.green
}
var body: some View {
...
}
}
otherwise check another approach shared here: https://stackoverflow.com/a/64727573/4977439
If that is systemImage, set the icon in systemName, then there is a mac app called SF Symbols where you can change the color to the emoji you want!

SwiftUI not detecting dark mode from setting

In SwiftUI on my device, I set the appearance to dark mode. However, my application doesn't change the background colour based on that appearance. For the color asset, I have set the values for both the light and dark appearance, like so
In my code this is how I set the background color and static variable,
extension Color {
static let backgroundColor = Color("Background")
}
..... // In my View
ZStack {
Color.backgroundColor
.ignoresSafeArea()
......
}
Also when I debug and check
#Environment (\.colorScheme) var colorScheme:ColorScheme
it returns that my device is indeed on dark mode. Any idea what is going on and why I'm unable to activate dark mode automatically from the user's device preferences.
Turns out when you use
.preferredColorScheme(.light | .dark)
It applies that preference to the entire view rather than the element the property is being used on.
This code works fine on simulator and device:
struct ColorSchemeView: View {
var body: some View {
Color.backgroundColor
.ignoresSafeArea()
}
}
fileprivate extension Color {
static let backgroundColor = Color("DarkLight")
}
I suspect you have some other issue in the code that will need a true Minimal Reproducible Example (MRE) to debug.

why toolbar text color always white in SwiftUI project

I have a problem in my SwiftUI project. In toolbar text buttons color is white, even I add this code
init() {
UIToolbar.appearance().barTintColor = UIColor.red
}
there is not any change, I do not know why? Any idea?
You could set an accent color to the view like this:
.accentColor(Color.red)

How to implement DarkMode Into app in Swift

sorry if that question was asked but couldn't find the right answer across stackOverFlow so I'm asking ..
I'm trying to implement dark mode into my app, but unfortunately it doesn't work well for me while using tableviews, it does changes my background and stuff, but I can't change the color of my groups in my tableview.
Here's an image to illustrate the problem:
https://imgur.com/a/h4A3zOZ (can't upload it here cause its too big).
Also Here is my Code:
// MARK: - Premium Section - DarkMode + Graph:
#IBAction func darkModeSwitch(_ sender: UISwitch) {
let current = sender.isOn ? Theme.dark : Theme.light
if #available(iOS 13.0, *) {
// overrideUserInterfaceStyle = UIUserInterfaceStyle(rawValue: current.stateMode)!
//STEP1: Saving User Defaults Switcher:
saveSwitchToggleDarkMode(switcherState: sender.isOn)
//STEP2: Setting UI Colors Of Settings View:
self.tableView.backgroundColor = current.backgroundColor
///Setting up the barTint Color:
self.navigationController?.navigationBar.barTintColor = current.barTintColor
///Setting up the title text color:
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:current.textColor]
///Changing back color in navigation controller:
self.navigationController?.navigationBar.backItem?.backBarButtonItem?.tintColor = current.backItemColor
}
}
You should change the mode on the window's level to apply changes to all your controls e.g:
if #available(iOS 13, *) {
UIApplication.shared.delegate?.window??.overrideUserInterfaceStyle = .dark
}
An alternative (and perhaps easier) method to implement dark mode is to use the iOS dark mode feature that you can trigger in settings.If you want to implement this you can create a custom color set by going to your Assets.xcassets and pressing the plus mark on the bottom -> new color set. On the attributes inspector, name your color under name, and under Appearances, select 'Any, Light, Dark' now you will have a place for 3 different colors. Under Light, put the light mode color, on the dark, the dark mode color.
Then on the place where you wish to implement this color,you can change the color to your custom color in the storyboard like so :-
or you can change it in code with something like
myButton.backgroundColor = UIColor(named: "TestColor")
When the user triggers the Dark mode through their control center or settings, the app will also automatically change accordingly. You can test this by going to settings -> Developer -> Dark appearance or by going to Features -> Toggle Appearance or simply press Shift + Command + A
However this method means that you will not have an independent dark mode because it will only be triggered if the device itself is dark-mode enabled.

Get update for light/dark mode theme change

I have a SwiftUI app, in which I have a SpriteKit scene. However, when changing between light and dark mode, the scene background does not change colour until the app is reopened.
This is not ideal, so I want to register when the light/dark mode appearance is changed. The colour in my assets has an "any" and a "dark" appearance.
Code inside updateUIView() within a UIViewRepresentable struct, which sets up the scene when it is linked with SwiftUI:
scene.backgroundColor = UIColor(named: "Color.Scene")!
uiView.presentScene(scene)
How do I achieve this? I am using iOS 13's system wide dark mode, so I do not want a solution of Notification Center because all I am looking to do is update the background according to the system settings.
You can use #Environment (\.colorScheme) var colorScheme: ColorScheme in your view and make the views body react to it:
struct ContentView: View {
#Environment (\.colorScheme) var colorScheme: ColorScheme
var body: some View {
HStack {
if self.colorScheme == .light {
// draw for light mode
} else {
// draw for dark mode
}
}
}
}