Make a VStack fill the width of the screen in SwiftUI

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

    One more alternative is to place one of the subviews inside of an HStack and place a Spacer() after it:

    struct ContentView : View {
        var body: some View {
            VStack(alignment: .leading) {
    
                HStack {
                    Text("Title")
                        .font(.title)
                        .background(Color.yellow)
                    Spacer()
                }
    
                Text("Content")
                    .lineLimit(nil)
                    .font(.body)
                    .background(Color.blue)
    
                Spacer()
                }
    
                .background(Color.red)
        }
    }
    

    resulting in :

提交回复
热议问题