简体   繁体   中英

How to horizontally center a view in a VStack

I am trying to make this view:

在此处输入图像描述

I have some code to make a triangle

struct Triangle: View {

    @State var height: CGFloat = 300
    @State var width: CGFloat = 300

    var body: some View {
        Path { path in
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: width / 2.0, y: height))
            path.addLine(to: CGPoint(x: width, y: 0))
        }
    }
}

struct Triangle_Previews: PreviewProvider {
    static var previews: some View {
        Triangle(height: 200, width: 300)
            .previewLayout(.sizeThatFits)
    }
}

That looks good:

在此处输入图像描述

However I'm having trouble centering the triangle in the vertical stack. I have commented out my failed attempts.

import SwiftUI

struct AccentView: View {

   //  var proxy: GeometryProxy!

    var body: some View {
        VStack(
         //   alignment: .center,
            spacing: 0
        ) {
            EmphasizeButton()
            Triangle(height: 10, width: 10)
                //.frame(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                //.alignmentGuide(.center)
                // .padding([.leading, .trailing])
//                .alignmentGuide(.center, computeValue: {
//                    _ in return -self.proxy.size.width / 2.0
//                })
                .foregroundColor(.green)
        }
    }
}

struct AccentView_Previews: PreviewProvider {
    static var previews: some View {
        AccentView()
    }
}

This is my result:

在此处输入图像描述

I think you'll have better luck with Triangle as a Shape and defining a frame for it. Right now, it's expanding to take up the available width:

struct Triangle : Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: rect.size.width / 2.0, y: rect.size.height))
            path.addLine(to: CGPoint(x: rect.size.width, y: 0))
        }
    }
}

struct ContentView: View {
    var body: some View {
        VStack(alignment: .center, spacing: 0) {
            Rectangle()
                .foregroundColor(.green)
                .frame(height: 30)
            Triangle().frame(width: 10, height: 10)
            .foregroundColor(.green)
        }
    }
}

The horizontal centering is done with alignment: .center -- you could also do it with an HStack with Spacer s:

struct ContentView: View {
    var body: some View {
        VStack(spacing: 0) {
            Rectangle()
                .foregroundColor(.green)
                .frame(height: 30)
            HStack {
                Spacer()
                Triangle().frame(width: 10, height: 10)
                    .foregroundColor(.green)
                Spacer()
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM