Make a VStack fill the width of the screen in SwiftUI

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

    This is a useful bit of code:

    extension View {
        func expandable () -> some View {
            ZStack {
                Color.clear
                self
            }
        }
    }
    

    Compare the results with and without the .expandable() modifier:

    Text("hello")
        .background(Color.blue)
    

    -

    Text("hello")
        .expandable()
        .background(Color.blue)
    

    0 讨论(0)
  • 2020-12-07 17:55

    You can do it by using GeometryReader

    GeometryReader

    Code:

    struct ContentView : View {
        var body: some View {
            GeometryReader { geometry in
                VStack {
                   Text("Turtle Rock").frame(width: geometry.size.width, height: geometry.size.height, alignment: .topLeading).background(Color.red)
                }
            }
        }
    }
    

    Your output like:

    0 讨论(0)
  • 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 :

    0 讨论(0)
  • 2020-12-07 17:57

    This is what worked for me (ScrollView (optional) so more content can be added if needed, plus centered content):

    import SwiftUI
    
    struct SomeView: View {
        var body: some View {
            GeometryReader { geometry in
                ScrollView(Axis.Set.horizontal) {
                    HStack(alignment: .center) {
                        ForEach(0..<8) { _ in
                            Text("                                                                    
    0 讨论(0)
  • 2020-12-07 17:57

    You can use GeometryReader in a handy extension to fill the parent

    extension View {
        func fillParent(alignment:Alignment = .center) -> some View {
            return GeometryReader { geometry in
                self
                    .frame(width: geometry.size.width,
                           height: geometry.size.height,
                           alignment: alignment)
            }
        }
    }
    

    so using the requested example, you get

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

    (note the spacer is no longer needed)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题