NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Changing to dark mode

Forums > SwiftUI

Hi, I have used @AppStorage to allow the user to select a toggle that forces 'dark mode'. I know that I can use the following modifier below to force dark mode for a view, but I also know I can't wrap a modifier in an if statement to set dark mode if the toggle is true.


I tried:

if toggle == true {

Any ideas how I can achieve this?


SwiftUI is declarative! Tell SwiftUI what you want.

Also, what is toggle? Please consider using a more descriptive name!

// Here, the userPrefersDarkMode is a boolean.
// If so, the parameter is .dark   otherwise it's .fancy
.preferredColorScheme(  userPrefersDarkMode ? .dark : .fancy )


if userPrefersDarkMode == true {


@flaneur7508 Why not let the user choose in their device settings and design the app that respects thier choice.

By putting this in what the default mode? .light! If so then the user who has dark mode on will see the app in light at first. If default is .dark then a user who has light mode will only see it dark mode at first. Until they work out how to change it, just let the system do it nad just make sure the app looks good in both!


Thank you @Obelix, that makes sense. I've replaced toggle. @NigelGee, I see what you mean but i've also seen other apps that allow the user to force dark mode. In my case I rather like the way it looks in Dark mode and perhaps i'd like to use that mode for the app all of the time regardless of the system settings. And Yes, i've designed the app to look fine in both modes.


@Obelix I do believe you are being mischievous there :)


@flaneur pushes aside my demons then peers into my soul:

@Obelix I do believe you are being mischievous there :)

Perhaps! But realize my intentions. You've named some boolean variable toggle! There are SwiftUI elements also named Toggle. I think the names are confusing because, without context, are you asking me about an interface object? or your variable?

To me, it's like naming your pets "Dog" and "Cat". Or perhaps naming your child, "Boy". Sure, he's a boy. And yeah, you have one dog and one cat. But really! There are better names for children and pets!

Back in the early days of Basic, integer variables were often one letter long and in the set between i and n.
i=10, j=42 etc. But they really didn't describe what they held, did they?

When I asked you to consider giving your boolean toggle a better name, I only want to guide you towards better practices.


Im new to this thanks for asking and helping newbies on here.


Thanks @Obelix, I fully understand your intentions and agree. I've updated the code accordingly.


Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.