如果在 SwiftUI 中使用 NavigationView,如何更改背景颜色? [英] How change background color if using NavigationView in SwiftUI?

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

问题描述

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

I want to change background color for full screen. I am using NavigationView, and I want to set Gray color for background (not default white)

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

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

Setting .background(Color.red) does not work in any place.

预览

推荐答案

如果你只是将你的内容嵌入到 ZStackNavigationView 中,你应该能够抛出在您的主要内容下方涂上颜色.

If you just embed your content in the NavigationView within a ZStack you should be able to throw the color in underneath your main content.

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

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

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