VStack的框架大小无法覆盖整个屏幕

时间:2019-08-13 15:06:25

标签: ios swift swiftui

我正在使用SwiftUI创建一些View,并且遇到了VStack框架尺寸的奇怪错误。渐变不会填充屏幕的所有高度。 这仅仅是Xcode错误还是我做错了什么? enter image description here

struct WelcomeView : View {

    private let colors = [
        Color(red: 29/255, green: 151/255, blue: 108/255),
        Color(red: 147/255, green: 249/255, blue: 185/255)
    ]

    let screenSize = UIScreen.main.bounds

    var body: some View {
        VStack {
            Text("Content here")
        }
        .frame(width: screenSize.width, height: screenSize.height)
        .background(LinearGradient(gradient: Gradient(colors: colors), 
   startPoint: .bottom, endPoint: .top), cornerRadius: 0)

    }
}

2 个答案:

答案 0 :(得分:0)

我没有设置框架,而是在文本的两边使用间隔符,但现在无法测试,但是请告知是否有帮助。

var body: some View {
        HStack{
            Spacer()
            VStack {
                Spacer()
                Text("Content here")
                Spacer()
            }
            Spacer()
        }
        .background(LinearGradient(gradient: Gradient(colors: [colors),
            startPoint: .bottom, endPoint: .top))
                .edgesIgnoringSafeArea(.all)
    }

答案 1 :(得分:0)

var body: some View {
    VStack {
        Spacer()
        Text("Content here")
        Spacer()
    }
    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
    .background(LinearGradient(gradient: Gradient(colors: colors), startPoint: .bottom, endPoint: .top), cornerRadius: 0)
    .edgesIgnoringSafeArea(.all)

}

来源:https://www.hackingwithswift.com/quick-start/swiftui/how-to-place-content-outside-the-safe-area