SwiftUI-无法填充工作表底部的屏幕

时间:2020-06-18 21:11:43

标签: swiftui

由于某种原因,我的纸的底部没有充满颜色。尝试了以下代码,但只能填充顶部安全空间。有任何线索吗?

Sheet not filling up with color

var body: some View {
    GeometryReader { geometry in
        VStack {
            Text("Hi")
                .font(.title)
        }
        .frame(width: geometry.size.width, height: geometry.size.height)
        .background(LinearGradient(gradient: Gradient(colors: [Color(hex: 0x71A2B6), Color(hex: 0xF09D51)]), startPoint: .top, endPoint: .bottom))
        .edgesIgnoringSafeArea(.all)
    }
}

2 个答案:

答案 0 :(得分:2)

您设置的.frame(width: geometry.size.width, height: geometry.size.height)不会覆盖底部区域。

如果您的目标只是让“视图”填满整个屏幕,则可能不需要GeometryReader

您可以改用ZStack

struct ContentView: View {
    var body: some View {
        ZStack {
            LinearGradient(gradient: Gradient(colors: [Color(hex: 0x71A2B6), Color(hex: 0xF09D51)]), startPoint: .top, endPoint: .bottom)
            VStack {
                Text("Hi")
                    .font(.title)
            }
        }
        .edgesIgnoringSafeArea(.all)
    }
}

答案 1 :(得分:1)

我向下移动.edgesIgnoringSafeArea(.all)使其覆盖GeometryRender,以使其充满设备,从而提供正确的屏幕尺寸。

import SwiftUI

struct ContentView: View {

var body: some View {
    GeometryReader { geometry in
        VStack {
            Text("Hi")
                .font(.title)
        }
        .frame(width: geometry.size.width,
               height: geometry.size.height)
        .background(LinearGradient(gradient: Gradient(colors: [Color.red, Color.blue]),
                                   startPoint: .top,
                                   endPoint: .bottom))

    }.edgesIgnoringSafeArea(.all)
    }
}

enter image description here