简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM