User: How to enable Dark mode by default? I'm using SwiftUI btw.
If you're using SwiftUI and can't see the mentioned options, it's possible that you're working with a SwiftUI project created using the SwiftUI App lifecycle. In this case, the structure of the project is different, and you won't find a traditional "Info.plist" file or the "Build Phases" tab.
To enable Dark mode by default in a SwiftUI App lifecycle project, follow these steps:
Open your project in Xcode.
In the Project Navigator, locate the "Assets.xcassets" folder and click on it.
In the Assets catalog, click the "+" button at the bottom and choose "New Color Set".
Name the new color set "AccentColor".
Select the "AccentColor" color set, and in the Attributes Inspector on the right, change the "Appearances" dropdown to "Any, Dark".
Set the desired accent color for both "Any Appearance" and "Dark Appearance".
Now, open the "App.swift" file or the file containing your main App struct.
Inside the main App struct, add the following code to the body
property:
@main
struct YourAppName: App {
var body: some Scene {
WindowGroup {
ContentView()
.preferredColorScheme(.dark) // Add this line to enable Dark mode by default
}
}
}
Replace "YourAppName" with the name of your app, and make sure to add .preferredColorScheme(.dark)
to the ContentView()
line.
Now, Dark mode will be enabled by default in your SwiftUI App lifecycle project.
User: there was already an AccentColor Color set
If there is already an "AccentColor" color set in your Assets catalog, you can skip the steps to create a new one. Instead, just make sure that the "Appearances" dropdown is set to "Any, Dark" and set the desired accent color for both "Any Appearance" and "Dark Appearance".
Then, proceed with the remaining steps:
Open the "App.swift" file or the file containing your main App struct.
Inside the main App struct, add the following code to the body
property:
@main
struct YourAppName: App {
var body: some Scene {
WindowGroup {
ContentView()
.preferredColorScheme(.dark) // Add this line to enable Dark mode by default
}
}
}
Replace "YourAppName" with the name of your app, and make sure to add .preferredColorScheme(.dark)
to the ContentView()
line.
Now, Dark mode will be enabled by default in your SwiftUI App lifecycle project.