Make a VStack fill the width of the screen in SwiftUI

前端 未结 14 1683
失恋的感觉
失恋的感觉 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:45

    An alternative stacking arrangement which works and is perhaps a bit more intuitive is the following:

    struct ContentView: View {
        var body: some View {
            HStack() {
                VStack(alignment: .leading) {
                    Text("Hello World")
                        .font(.title)
                    Text("Another")
                        .font(.body)
                    Spacer()
                }
                Spacer()
            }.background(Color.red)
        }
    }
    

    The content can also easily be re-positioned by removing the Spacer()'s if necessary.

提交回复
热议问题