Make a VStack fill the width of the screen in SwiftUI

前端 未结 14 1681
失恋的感觉
失恋的感觉 2020-12-07 16:58

Given this code :

import SwiftUI

struct ContentView : View {
    var body: some View {
        VStack(alignment: .leading) {
            Text(\"Title\")
            


        
14条回答
  •  暖寄归人
    2020-12-07 17:59

    Try using the .frame modifier with the following options:

    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
    
    struct ContentView: View {
        var body: some View {
            VStack(alignment: .leading) {
                Text("Hello World").font(.title)
                Text("Another").font(.body)
                Spacer()
            }.frame(minWidth: 0,
                    maxWidth: .infinity,
                    minHeight: 0,
                    maxHeight: .infinity,
                    alignment: .topLeading
            ).background(Color.red)
        }
    }
    

    This is described as being a flexible frame (see the documentation), which will stretch to fill the whole screen, and when it has extra space it will center its contents inside of it.

提交回复
热议问题