UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

   .preferredColorScheme(.dark)

I tried:

if toggle == true {
   .preferredColorScheme(.dark)
   }

Any ideas how I can achieve this?

1      

SwiftUI is declarative! Tell SwiftUI what you want.

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

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

versus

// PROCEDUREAL APPROACH
if userPrefersDarkMode == true {
   .preferredColorScheme(.dark)
}

1      

@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!

1      

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.

1      

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

1      

@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.

1      

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

1      

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

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED 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! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.