SwiftUI:如何更改警报按钮和导航链接返回按钮的颜色?

5
如何更改“警告”中按钮的颜色以及NavigationLink的后退按钮的颜色?在“警告”按钮文本后设置 .accentColor(.init("someColor")) 无效。在navigationLink之后设置 .accentColor(.init("someColor")) 也不起作用。我该怎么办?
“警告”按钮: enter image description here NavigationLink的返回按钮: enter image description here
struct ContentView: View {
    
    @State var showSheet = false
    @State var alertShouldBeShown = !UserDefaults.standard.bool(forKey: "£Start")

    var body: some View {
        Button(action:  {
            self.showSheet.toggle()
        }) {
            Text("click me")
            //.accentColor(colorScheme == .dark ? Image("") : Image("Info-Icon"))
        }.sheet(isPresented: $showSheet) {
            SheetView(showSheet: self.$showSheet)
        }
        .alert(isPresented: $alertShouldBeShown, content: {

            Alert(title: Text("Headline"),
                message: Text("Text"),
                dismissButton: Alert.Button.default(
                    Text("Ok"), action: {
                    UserDefaults.standard.set(true, forKey: "Start")
                    }
                )
            )
        })
    }
}

struct SheetView: View {
    @Binding var showSheet: Bool
    var body: some View {
        NavigationView {
            DetailView()
                .navigationBarTitle(Text("Headline"), displayMode: .inline)
                .navigationBarItems(trailing: Button(action: {
                    self.showSheet = false
                }) {
                    Text("Done")
                        .bold()
            })
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

struct DetailView: View {
    var body: some View {
        List {
            HStack {
                NavigationLink(destination: DetailViewOne()) {
                                    Text("View 1")
                }
            }
            HStack {
                NavigationLink(destination: DetailViewTwo()) {
                                    Text("View 2")
                }
            }
        }
    }
}

struct DetailViewOne: View {
    var body: some View {
        Text("1")
    }
}

struct DetailViewTwo: View {
    var body: some View {
        Text("2")
    }
}

这个回答是否解决了你的问题?https://dev59.com/ALnoa4cB1Zd3GeqPQW5b#60381525 - Asperi
这个解决方案并不能回答我的问题。 - lula08
2个回答

15
您可以使用accentColor来更改NavigationBar的突出色,但是您需要将其应用于SheetView(给定环境的根视图):
SheetView(showSheet: self.$showSheet)
    .accentColor(.red)

不幸的是,到目前为止,SwiftUI并没有允许太多Alert的自定义。

然而,由于Alert是基于UIKit组件(UIAlertController)构建的,这也意味着您可以在包含在UIAlertController中的UIView中更改外观

您可以将此代码放在@main App init或SceneDelegate中:

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

1
不错,一个代码行的修复可以适用于所有的UIKit和SwiftUI警告。 - Marcy

1
我建议不要在@main App init或SceneDelegate中调用UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor,因为这会改变应用中所有警告的tintColor。要更改特定警告的tintColor,请添加此ViewModifier和View的扩展。
extension View {
    func alertButtonTint(color: Color) -> some View {
        modifier(AlertButtonTintColor(color: color))
    }
}

struct AlertButtonTintColor: ViewModifier {
    let color: Color
    @State private var previousTintColor: UIColor?

    func body(content: Content) -> some View {
        content
            .onAppear {
                previousTintColor = UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor
                UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(color)
            }
            .onDisappear {
                UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = previousTintColor
            }
    }
}

这将在特定的警告中更改按钮的tintColor,而不会全局更改所有的tintColors。
用法:
struct AlertTest: View {
@State private var showAlert = false

    var body: some View {
        Button("Show Alert", action: { showAlert = true })
            .alert(isPresented: $showAlert) {
                Alert(title: Text("Alert Title"),
                      message: Text("Alert message"),
                      dismissButton: .default(Text("Reset Value"), action: { print("OK!!!") }))
            }
            .alertButtonTint(color: .orange)
    }
}

结果:

alertImage


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接