How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift

For the eleventh post of the #30DaysOfSwift series, I am adding a Dark Mode Toggle to switch between Light/Dark modes in SwiftUI.


This content originally appeared on HackerNoon and was authored by Vaibhav

Day 11: Embrace the Night 🌙

\ For the eleventh post of the #30DaysOfSwift series, I am adding a Dark Mode Toggle to switch between Light/Dark modes in SwiftUI.

\

SwiftUI natively supports Dark Mode, and today, we'll create a sleek, minimalistic UI to let users toggle between the two themes.

Steps to Add a Dark Mode Toggle:

1. Create a Toggle Button:

Add a toggle switch in the settings view that allows users to manually switch between light and dark modes.

\ 2. Customize the Appearance:

We'll implement a system that changes the app's color scheme using SwiftUI's @Environment(.colorScheme) modifier.

import SwiftUI

struct DarkModeToggleView: View { 

@State private var isDarkMode = false // State to track the mode

var body: some View {
    VStack {
        // Title Text
        Text("Dark Mode Toggle")
            .font(.largeTitle)
            .fontWeight(.bold)
            .padding()

        // Example image to showcase the mode effect
        Image(systemName: isDarkMode ? "moon.fill" : "sun.max.fill")
            .resizable()
            .scaledToFit()
            .frame(width: 100, height: 100)
            .foregroundColor(isDarkMode ? .yellow : .orange) // Custom color for each mode
            .padding()

        // Toggle switch with label
        Toggle(isOn: $isDarkMode) {
            Text(isDarkMode ? "Dark Mode" : "Light Mode")
                .font(.headline)
        }
        .toggleStyle(SwitchToggleStyle(tint: .blue)) // Beautiful blue accent color
        .padding()

    }
    // Apply the color scheme dynamically
    .preferredColorScheme(isDarkMode ? .dark : .light)
    .animation(.easeInOut, value: isDarkMode) // Smooth transition between modes
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(isDarkMode ? Color.black : Color.white) // Set background color based on mode
    .edgesIgnoringSafeArea(.all)
}
}

struct DarkModeToggleView_Previews: PreviewProvider { static var previews: some View { DarkModeToggleView() } }

\ Add this feature to your app, and give your users a sleek way to switch between Light and Dark Mode! 🌗

\ Happy Coding!

\ P.S. This series is becoming huge! You can read about all the stories by clicking on my profile.


This content originally appeared on HackerNoon and was authored by Vaibhav


Print Share Comment Cite Upload Translate Updates
APA

Vaibhav | Sciencx (2024-10-22T21:54:58+00:00) How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift. Retrieved from https://www.scien.cx/2024/10/22/how-to-add-a-dark-mode-toggle-for-ios-18-30daysofswift/

MLA
" » How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx - Tuesday October 22, 2024, https://www.scien.cx/2024/10/22/how-to-add-a-dark-mode-toggle-for-ios-18-30daysofswift/
HARVARD
Vaibhav | Sciencx Tuesday October 22, 2024 » How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift., viewed ,<https://www.scien.cx/2024/10/22/how-to-add-a-dark-mode-toggle-for-ios-18-30daysofswift/>
VANCOUVER
Vaibhav | Sciencx - » How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/22/how-to-add-a-dark-mode-toggle-for-ios-18-30daysofswift/
CHICAGO
" » How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx - Accessed . https://www.scien.cx/2024/10/22/how-to-add-a-dark-mode-toggle-for-ios-18-30daysofswift/
IEEE
" » How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx [Online]. Available: https://www.scien.cx/2024/10/22/how-to-add-a-dark-mode-toggle-for-ios-18-30daysofswift/. [Accessed: ]
rf:citation
» How to Add a Dark Mode Toggle for iOS 18 – #30DaysOfSwift | Vaibhav | Sciencx | https://www.scien.cx/2024/10/22/how-to-add-a-dark-mode-toggle-for-ios-18-30daysofswift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.