简体   繁体   English

SwiftUI:整个屏幕的背景颜色而不是简单的视图?

[英]SwiftUI: background color for whole screen and not simple view?

I am trying to do something very simple, display a view with a background color, and in the center of this view, display a single label.我正在尝试做一些非常简单的事情,显示一个带有背景颜色的视图,并在此视图的中心显示一个标签。

I tried this:我试过这个:

var body: some View {
    VStack {
        Text("Hello!")
    }
    .background(MyColors.blue)
    .ignoresSafeArea() 
}

Here is the given result:这是给定的结果:

在此处输入图片说明

What am I doing wrong?我究竟做错了什么?

Thank you for your help感谢您的帮助

You can do it like this:你可以这样做:

Text("Hello!")
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(MyColors.blue)
    .ignoresSafeArea()

Source: https://www.hackingwithswift.com/articles/224/common-swiftui-mistakes-and-how-to-fix-them资料来源: https : //www.hackingwithswift.com/articles/224/common-swiftui-mistakes-and-how-to-fix-them

I managed to get it working with this code:我设法让它与此代码一起工作:

var body: some View {
    ZStack {
        Style.Color.red
        VStack {
            Text("Hello!")
        }
    }
    .ignoresSafeArea()
}

I didn't understand first that background and rest of the view must be set on a different z-axis.我首先不明白背景和视图的其余部分必须设置在不同的 z 轴上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 swiftUI 中视图的背景颜色 - Change the background color of a view in swiftUI 带有 ScrollView 和导航栏 SwiftUI 的屏幕背景颜色 - Screen Background Color With ScrollView And Navigation Bar SwiftUI 根据 SwiftUI 中的另一个视图更改背景颜色? - Changing background color based on another View in SwiftUI? 无法更改 SwiftUI 中视图的背景颜色 - Unable to change background color of view in SwiftUI 在 swiftUI 中为包含 List/Tableview 的全屏添加背景颜色 - Adding background color for full screen containing List/Tableview in swiftUI SwiftUI 视图背景中的 UITextView 和暗模式(表单)的边框颜色 - UITextView inside a SwiftUI view background and border color for dark mode (Form) 如何在 SwiftUI 中将包含列表的视图的背景颜色更改回白色 - How to change Background Color of a View containing a List back to White in SwiftUI 为什么在 SwiftUI 中显示视图时列表的背景颜色不同? - Why background color of List is different while presenting view in SwiftUI? SwiftUI - 如何模糊视图的默认背景颜色? - SwiftUI - How can I blur the default background color of a view? 在 SwiftUI 中更改具有 NavigationView 和 ScrollView 的 TabView 内视图的背景颜色 - Change background color of View inside TabView having NavigationView and ScrollView in SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM