swift 如何在暗模式下显示确认对话框?

fnvucqvd  于 2023-08-02  发布在  Swift
关注(0)|答案(3)|浏览(156)

将应用程序切换到黑暗模式时,confirmationDialog显示为白色。如何使confirmationDialog也在黑暗模式下显示?

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)
                        }
                    }

字符串

00jrzges

00jrzges1#

您可能需要添加.preferredColorScheme()

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

字符串
差不多吧可能需要玩修改器在哪里。我从来没有使用过它,所以我不完全确定它会去哪里。

zfycwa2u

zfycwa2u2#

if colorScheme == .dark {

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .white

} else {

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .black

}

字符串
这是可行的,但它会使整个.confirmationDialog的颜色相同。

ewm0tg9j

ewm0tg9j3#

您正确阅读colorScheme
第一个月
但你却不用它代替变量isDarkMode,在颜色修改器中使用colorScheme == .dark,使视图适应配色方案。
在您的示例中:

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 // not neccessary
    @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(colorScheme ? .white : .black) // use colorScheme
                                .frame(width: 28, height: 28)
                                .background(colorScheme ? 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))) // use colorScheme
                                .clipShape(Circle())
                                .overlay {
                                    Circle().stroke(isDarkMode ? .black : Color(.secondarySystemBackground), lineWidth: 4)
                                }
                                .scaleEffect(x: 1.1, y: 1.1, anchor: .center)
                        }
                    }

字符串

  • —-

不阅读环境值colorScheme的一个更简单的选项是将颜色选择留给具有语义颜色的系统:

.foregroundColor(.primary)


在这种情况下省略.background(Color...)

相关问题