更改导航栏颜色

时间:2019-06-27 04:24:54

标签: uinavigationbar swiftui

我正在学习swiftui,我写了一个非常简单的清单,像这样。

NavigationView {

    List {
        Toggle(isOn: $isFemale) {
            Text("Voice: \(self.isFemale == true ? "Female":"Male")").font(.system(size: 17)).bold()
            }.padding(4)

        NavigationButton(destination: LanguagePage()) {
            VStack(alignment: .leading) {
                Text("Change Language").font(.system(size: 17)).bold().padding(4)
                Text("English - English").font(.system(size: 17)).font(.system(.body)).padding(4)
            }
        }


        .navigationBarTitle(Text("Settings"), displayMode: .large)}

如何更改导航栏颜色?目前,它是半透明的。如何更改为不透明?

2 个答案:

答案 0 :(得分:2)

您可以使用外观API更改为导航栏标题和背景颜色


init() { // for navigation bar title color
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
// For navigation bar background color 
UINavigationBar.appearance().backgroundColor = .green
   }     
NavigationView {

    List {
        Toggle(isOn: $isFemale) {
            Text("Voice: \(self.isFemale == true ? "Female":"Male")").font(.system(size: 17)).bold()
            }.padding(4)

        NavigationButton(destination: LanguagePage()) {
            VStack(alignment: .leading) {
                Text("Change Language").font(.system(size: 17)).bold().padding(4)
                Text("English - English").font(.system(size: 17)).font(.system(.body)).padding(4)
            }
        }
        .navigationBarTitle(Text("Settings"), displayMode: .large)}

答案 1 :(得分:2)

使用XCode 11.0 beta 5(11M382q),我可以使用以下代码:

struct TableWithNavTitle: View {

    var body: some View {
        // For navigation bar background color
        UINavigationBar.appearance().backgroundColor = .red

        return NavigationView {
            List(0..<5) { item in
                NavigationLink("\(item)", destination: Text("\(item)"))
            }.navigationBarTitle("Title")
        }
    }
}