【问题标题】:How change background color if using NavigationView in SwiftUI?如果在 SwiftUI 中使用 NavigationView,如何更改背景颜色?
【发布时间】:2019-11-17 06:55:33
【问题描述】:

我想更改全屏的背景颜色。我正在使用NavigationView,我想为背景设置灰色(不是默认白色)

struct ContentView : View {
    var body: some View {
        NavigationView {
            ScrollView {
                Text("Example")
            }
            .navigationBarTitle("titl")
        }
    }
}

设置.background(Color.red)在任何地方都不起作用。

preview

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    如果您只是将您的内容嵌入到 NavigationView 中的 ZStack 中,您应该能够将颜色放在主要内容的下方。

    struct ContentView : View {
        var body: some View {
            NavigationView {
                ZStack {
                    Color.red.edgesIgnoringSafeArea(.all)
                    ScrollView {
                        Text("Example")
                    }
                    .navigationBarTitle("title")
                }
            }
        }
    }
    

    【讨论】:

    • 就我而言,乍一看这是可行的,但我发现它会使我的应用程序崩溃。我的“ContentView”已经在 ZStack 中,当这个“ContentView”从树中移除时会发生崩溃。更准确地说,是在颜色上使用“edgesIgnoringSafeArea”导致我的应用程序崩溃。所以现在,我仍然需要找到一种改变导航栏颜色的有效方法。恐怕暂时没有解决办法……
    【解决方案2】:

    添加到 Mattis Schulte 的回答中,我遇到的副作用之一是状态栏不会继承背景颜色。

    但是,当您将列表(例如)向上滚动到视图顶部并且 iOS 切换到内联标题视图(带有居中的 NavigationBarTitle)时,它会在状态栏区域着色,从而留下相当不受欢迎的用户体验。

    我能够使用的解决方法是:

    import SwiftUI
    
    let coloredNavAppearance = UINavigationBarAppearance()
       
    struct ListView: View {
    
        init() {
            coloredNavAppearance.configureWithOpaqueBackground()
            coloredNavAppearance.backgroundColor = .systemBlue
            coloredNavAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            coloredNavAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
                   
            UINavigationBar.appearance().standardAppearance = coloredNavAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = coloredNavAppearance
    
        }
    
        var body: some View {
            NavigationView {
                Form {
                    Section(header: Text("General")) {
                        HStack {
                            Text("ListView #1")
                            Spacer()
                            Text("data")
                                .multilineTextAlignment(.trailing)
                        }
                        HStack {
                            Text("ListView #2")
                            Spacer()
                            Text("data")
                                .multilineTextAlignment(.trailing)
                        }
                    }
    
                }
                .navigationBarTitle("NavBar Title")
            }
        }
    }
    
    
    struct ListView_Previews: PreviewProvider {
        static var previews: some View {
            ListView()
        }
    }
    
    

    希望这对其他人有所帮助。 感谢https://sarunw.com/posts/uinavigationbar-changes-in-ios13/

    【讨论】:

      【解决方案3】:

      最新的解决方案是扩展UINavigationController,如下所示:

      extension UINavigationController {
          override open func viewDidLoad() {
              super.viewDidLoad()
      
          let standard = UINavigationBarAppearance()
          standard.backgroundColor = .red //When you scroll or you have title (small one)
      
          let compact = UINavigationBarAppearance()
          compact.backgroundColor = .green //compact-height
      
          let scrollEdge = UINavigationBarAppearance()
          scrollEdge.backgroundColor = .blue //When you have large title
      
          navigationBar.standardAppearance = standard
          navigationBar.compactAppearance = compact
          navigationBar.scrollEdgeAppearance = scrollEdge
       }
      }
      

      或者,旧的:

      在你的结构初始化器中更改UITableView颜色,如下:

      struct ContentView : View {
      init() {
          UITableView.appearance().backgroundColor = .red
          }
      var body: some View {
          NavigationView {
              ScrollView {
                  Text("Example")
              }
              .navigationBarTitle("title")
          }
        }
      }
      

      【讨论】:

      • 这会为整个应用配置导航栏吗?
      • @aheze,是的,将其添加到 appdelegate 中。
      • UITableView 上也可以使用扩展解决方案吗?
      【解决方案4】:

      只需将其添加到代码 UIScrollView.appearance().backgroundColor = UIColor.red 的初始化程序中即可。但不幸的是,这种解决方案有很多副作用。

      【讨论】:

        【解决方案5】:

        您可以做的一个小技巧是添加一个其中没有任何文本的文本视图,并将背景颜色添加到您想要的内容中,如下所示:

        Text(" ")
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            .background(Color.red)
            .edgesIgnoringSafeArea(.all)
        

        【讨论】:

        • 它代码添加颜色swift NavigationView { ZStack { Text(" ") .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) .background(Color.red) .edgesIgnoringSafeArea(.all) ScrollView { ChooseView() ChooseView() } } .navigationBarTitle("Title") } 但滚动不正确(导航不翻译)
        • 视图没有滚动?您确定滚动视图位于文本视图之上吗?您可以上传您尝试滚动的数据的图片吗?另外,如果你删除了textview,你可以滚动吗?
        • 导航栏不从大状态滚动到基本状态
        • 天哪。我不敢相信只需要具有 backgroundColor 属性的东西就需要这种 hack。
        • 这是一个 hack.. 所以当你使用它时你应该预料到错误。如果您不希望出现问题...例如无法滚动,我会远离此解决方案。
        【解决方案6】:

        好的,我刚刚尝试了 omar 的解决方案,不幸的是,如果堆栈中有任何其他视图,它就不起作用。

        所以我现在看到的最好的答案实际上改变了NavigationView的底层背景颜色(在OP的原始问题中)只是在UIView外观上设置背景颜色,正如in this answer所没有的那样:

        UIView.appearance().backgroundColor = UIColor.red
        

        【讨论】:

        • 当我尝试您的解决方案时,我得到了所需的背景颜色,但其他一切都消失了。
        • 我也遇到了与@MattisSchulte 相同的问题。
        【解决方案7】:

        这是我的解决方案,因为其他人都没有在我的情况下工作,所以我想我会分享..

        所以对于导航标题栏颜色,添加如下初始化器:

        struct ContentView: View {
            init() {
                UINavigationBar.appearance().backgroundColor = .red
            }
            var body: some View {
                NavigationView {
                    ScrollView {
                        ...
                    }
                }
            }
        }
        

        然后要编辑 ScrollView 背景颜色,您可以执行以下操作: 我已经为 ListView 做了,但你可以为 ScrollView 做。

        struct ListBackgroundColor: ViewModifier {
        
            let color: UIColor
        
            func body(content: Content) -> some View {
                content
                    .onAppear() {
                        UITableView.appearance().backgroundColor = self.color
                        //(Optional) Edit colour of cell background
                        UITableViewCell.appearance().backgroundColor = self.color
                    }
            }
        }   
        
        extension View {
            func listBackgroundColor(color: UIColor) -> some View {
                ModifiedContent(content: self, modifier: ListBackgroundColor(color: color))
            }
        
        }
        

        用户界面示例https://imgur.com/a/TQ9c5Sc

        【讨论】:

          【解决方案8】:

          更改 NavigationView 的背景颜色并保持其正常工作(如在内容滚动期间缩小/放大)以及保持安全区域非常复杂

          NavigationView-color-customisation on github 在这里我发布了关于在 SwiftUI 应用中自定义颜色的常见问题和解决方案

          如果你真的想改变所有 navgiationViews 中的 bg 颜色,prev 评论的第一部分关于 UINavigationController 上的 Extension 看起来不错

          【讨论】:

            【解决方案9】:

            SwiftUI 中的NavigationView 和UIKit 中的UINavigationBar 一样繁琐,我有iOS 系统导航栏的第三方开源解决方案,希望能帮助你解决问题。

            Git 仓库:NXNavigationExtension

            更改导航视图颜色:

            struct ContentView: View {
                
                var body: some View {
                    NavigationView {
                        NavigationLink {
                            Text("Pop")
                                .padding()
                                .useNXNavigationView(onPrepareConfiguration: { configuration in
                                    // Page2
                                    configuration.navigationBarAppearance.backgroundColor = .green
                                })
                        } label: {
                            Text("Push")
                                .padding()
                                .useNXNavigationView(onPrepareConfiguration: { configuration in
                                    // Page1
                                    configuration.navigationBarAppearance.backgroundColor = .red
                                })
                        }
                    }
                    .navigationViewStyle(.stack)
                }
                
            }
            

            ?example

            【讨论】:

              猜你喜欢
              • 2020-01-01
              • 1970-01-01
              • 2020-10-11
              • 1970-01-01
              • 1970-01-01
              • 2019-12-21
              • 2021-02-11
              • 2020-02-19
              • 2022-08-12
              相关资源
              最近更新 更多