【问题标题】:How To Set NavigationView Background Colour in SwiftUI如何在 SwiftUI 中设置 NavigationView 背景颜色
【发布时间】:2019-12-21 20:30:21
【问题描述】:

我正在尝试设置NavigationView 的背景颜色。我正在尝试使用下面的代码(部分来自 SwiftUI 教程)添加ZStack。然而,除非我将 NavigationView... 替换为 Spacer(),否则它只会是白色的

    var body: some View {
    ZStack
        {
            NavigationView {
                List {
                    Toggle(isOn: $userData.showFavoritesOnly) {
                        Text("Favourites")
                    }
                    ForEach(userData.landmarks) { landmark in
                        if !self.userData.showFavoritesOnly || landmark.isFavorite {
                            NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                                LandmarkRow(landmark: landmark)
                            }
                        }
                    }
                }

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

    }.background(Color.blue.edgesIgnoringSafeArea(.all))
}

我可以设置单个列表项的颜色,但我希望整个背景显示为蓝色

【问题讨论】:

  • 不允许编辑问题并将其更改为完全不同的问题。请将其改回原来的并提出另一个问题。
  • 同样的问题。澄清我是如何尝试改变背景的
  • 看看由于不同的说明,答案有何不同?希望对您有所帮助。
  • 我看到我对这个问题的澄清帮助你理解了:)
  • 看看我的例子here

标签: ios swift xcode swiftui


【解决方案1】:

UINavigationBar 相同。但是由于还没有直接的api,你可以使用appearance来改变它:

UINavigationBar.appearance().backgroundColor = .orange
UINavigationBar.appearance().tintColor = .green
UINavigationBar.appearance().barTintColor = .yellow
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.red]
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.red]

您应该将它放在您确定编译器在init() 方法中读取的位置。

请注意,其中一些将在 Xcode 11 beta 5 下无法使用

【讨论】:

  • 感谢这帮助,几乎是我所需要的。为 UIView 交换 UINavigationBar 并且所有背景都改变了
  • 切换到 UIView 会改变背景,但 swiftUI 内容会显示。你找到让它出现了吗?
  • 也许如果您发布具有可重现问题的最小工作代码,我可以找到一种方法;)@RichardWitherspoon。在这里发布问题并评论它的链接以通知我,我会尽力帮助你。
  • @MojtabaHosseini 这是我试图通过查看这个问题来解决的问题的链接stackoverflow.com/questions/57835597/…
【解决方案2】:

这似乎是使导航视图透明的关键:UITableView.appearance().backgroundColor = .clear

感谢:https://izziswift.com/swiftui-list-color-background/ 的 sn-p。

这是我使用 Xcode 13 Beta 针对 iOS 15 Beta 进行测试的代码:Screenshot ...还使用 Xcode 13 Beta 部署到 iPhone 12 iOS 14.7 进行测试。

它可以与 Xcode 12.x 和 iOS 14.x 一起使用(请参阅有关 SwiftUI 3 功能的 cmets 您可能会删除等)

//  NavigationView.swift
//  swiftui.proto2
//
//  Created by Sunil Raman on 12/8/21
//  Updated by Sunil Raman on 18/8/21

import SwiftUI

//Testing gradient backgrounds, etc. (may require Xcode 13)
//Credit: https://sarunw.com/posts/how-to-set-screen-background-color-in-swiftui/
let backgroundGradient = LinearGradient(
    colors: [Color.purple, Color.red],
    startPoint: .top, endPoint: .bottom)

//Build view here!
struct MyCoolListView: View {
    
    //The "magic" happens here
    //Credit: https://izziswift.com/swiftui-list-color-background/
    init() {
        //Appears to be the key to making Navigation View transparent
        UITableView.appearance().backgroundColor = .clear
        //Just for reference, not sure if opacity can be adjusted
        UINavigationBar.appearance().barTintColor = .white
    }
        
    //Our view, of course
    var body: some View {
      
        //Our navigation view
        NavigationView {
            
            //Our list
            List() {
            
                //Testing sections w.r.t. layout purposes, formatting section headers, etc.
                Section(header: Text("Section 1").font(.title2).foregroundColor(.yellow)) {
                    NavigationLink("Text 1", destination: MyCoolListView())
                    NavigationLink("Text 1", destination: MyCoolListView())
                    NavigationLink("Text 1", destination: MyCoolListView())
                }
                
                Section(header: Text("Section 2").font(.title3).fontWeight(.bold)) {
                    NavigationLink("Text 2", destination: MyCoolListView())
                    NavigationLink("Text 2", destination: MyCoolListView())
                    NavigationLink("Text 2", destination: MyCoolListView())
                }
                        
                Section(header: Text("Section 3").font(.title3).fontWeight(.light)) {
                    NavigationLink("Text 3", destination: MyCoolListView())
                    NavigationLink("Text 3", destination: MyCoolListView())
                    NavigationLink("Text 3", destination: MyCoolListView())
                }
                        
                //For reference, you can uncomment this to test
                //.listRowBackground(Color.blue)
                        
           }
           .listStyle(.insetGrouped)
           //This simulates a "material" type effect, hard to apply to Lists even in SwiftUI 3?
           .opacity(0.65)
           //New SwiftUI 3 feature, you can use if #available(iOS 15, *) or something
           //.refreshable {
               //Refresh action here
           //}
           //Can the navigation title be manipulated further? Not sure.
           .navigationTitle(Text("Title"))
           //The awesome background!
           .background(backgroundGradient)
           //This helps with iOS 14.7 
           .ignoresSafeArea()
                                
        } //End NavigationView
        
    } //End some View
    
} //End struct


//Enabling the previewing in Xcode
struct MyCoolListView_Previews: PreviewProvider {
    static var previews: some View {
        if #available(iOS 15.0, *) {
            MyCoolListView()
                .previewInterfaceOrientation(.landscapeLeft)
        } else {
            // Fallback on earlier versions
        }
    }
}

【讨论】:

    【解决方案3】:

    这是我想出的解决方案...此解决方案还允许您将背景单元格着色为与列表元素不同的颜色。

    因为 ZStack 不起作用,并且 UIView/UINavigationBar.appearance().backgroundColor 会改变整个可见屏幕的颜色。

    我想我会添加这个,因为其他解决方案都不适合我。

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

    【讨论】:

    • 您的解决方案没有解决这个问题,特别是关于 NavigationView 而不是任何 View。
    猜你喜欢
    • 1970-01-01
    • 2020-02-19
    • 2020-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2019-11-17
    • 2019-12-24
    相关资源
    最近更新 更多