skip to Main Content

I am a beginner iOS developer. Faced a problem. When switching the application to dark mode, the confirmationDialog is displayed in white. How can I make the confirmationDialog also show up in dark mode? I would be grateful for help.

import SwiftUI

struct SettingListView: View, Themeable {
    @Environment(.dismiss) var dismiss
    @State private var showingConfirmation = false
    @Environment(.colorScheme) var colorScheme
    @AppStorage("isDarkMode") private var isDarkMode = false
    @StateObject var settingsListViewModel = SettingsListViewModel()
    @State private var changeValueID = true
    @State private var showContacts = true

var body: some View {
        NavigationStack {
            List {
                Section {
                    VStack {
                        ZStack(alignment: .bottomTrailing) {
                            Image("avatar")
                                .resizable()
                                .scaledToFill()
                                .frame(width: 120, height: 120)
                                .clipShape(Circle())
                                .onTapGesture {
                                    showingConfirmation = true
                                }
                                .confirmationDialog("Change Profile Picture", isPresented: $showingConfirmation, titleVisibility: .visible) {
                                    Button(role: .none, action: {}) {
                                        Text("Take Photo")
                                    }
                                    Button(role: .none, action: {}) {
                                        Text("Choose from library")
                                    }
                                    Button(role: .none, action: {}) {
                                        Text("Use Avatar")
                                    }
                                }
                            Image(systemName: "pencil")
                                .foregroundColor(isDarkMode ? .white : .black)
                                .frame(width: 28, height: 28)
                                .background(isDarkMode ? Color(#colorLiteral(red: 0.370555222, green: 0.3705646992, blue: 0.3705595732, alpha: 1)) : Color(#colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)))
                                .clipShape(Circle())
                                .overlay {
                                    Circle().stroke(isDarkMode ? .black : Color(.secondarySystemBackground), lineWidth: 4)
                                }
                                .scaleEffect(x: 1.1, y: 1.1, anchor: .center)
                        }
}

3

Answers


  1. You may want to to add the .preferredColorScheme()

    @State private var isDarkMode = false
      Image(Avatar)
       .confirmationDialog(.....) {
        VStack {
          ....
        }
        .preferredColorScheme(isDarkMode ? .dark : .light)
       }
       
    

    Something like that. May need to play with where the modifier is. I’ve never used it so Im not entirely sure where it would go.

    Login or Signup to reply.
  2. if colorScheme == .dark {
    
    UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .white
    
    } else {
    
    UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .black
    
    }
    

    This works but it would make your entire `.confirmationDialog of the same color.

    Login or Signup to reply.
  3. You‘re not using colorScheme that you are reading correctly with:

    @Environment(.colorScheme) var colorScheme

    But then, instead of your variable isDarkMode, use colorScheme == .dark to adapt your view to the color scheme.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search