如何在SwiftUI中设置NavigationView背景颜色 [英] How To Set NavigationView Background Colour in SwiftUI

查看:793
本文介绍了如何在SwiftUI中设置NavigationView背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置NavigationView的背景色.我正在尝试通过下面的代码(部分来自SwiftUI教程)添加ZStack.除非我将NavigationView...替换为Spacer()

I'm trying to set the background color of a NavigationView. I'm trying by adding a ZStack using the code below (partly from the SwiftUI tutorial). Yet it's only ever white unless I replace NavigationView... with 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))
}

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

I can set the individual list item color but i want the whole background to show blue

推荐答案

UINavigationBar相同.但是由于还没有直接的api,您可以使用appearance:

It is same as UINavigationBar. But since there is no direct api yet, you can change it using 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()方法内部.

You should put this somewhere that you sure the compiler reads like inside the init() method.

请注意,其中一些将在Xcode 11 beta 5以下版本中不起作用.

这篇关于如何在SwiftUI中设置NavigationView背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆