Xcode SwiftUI按钮-无法居中

我是SwiftUI的新手,我将为无法定位的自定义按钮的位置提供一些帮助。 我已经使用VStack和HStack尝试使按钮底部居中,但是按钮保持左对齐。 任何帮助将不胜感激。

struct ContentView: View {
    var body: some View {

        VStack {
            NavigationView {

                Section {
                VStack(alignment: .leading) {


                    Text("Meal Description").foregroundColor(.green)
                    Spacer()

                            NavigationLink(destination: poundedYam()) {
                                Text("pounded Yam & Egusi Soup")
                    }

                            NavigationLink(destination: YamPepSoup()) {
                                Text("Yam and Pepper Soup")
                            }


                        NavigationLink(destination: JollofRice()) {
                                Text("Jollof Rice & Plantain")
                        }

                    Spacer()
                        .frame(minWidth: 0,maxWidth: .infinity,minHeight: 0,maxHeight: .infinity,alignment: .topLeading)



                    }.padding()

                }


                Section {

                    Image("africanfoods")
                        .resizable()
                        .frame(width: 275.0,height: 250.0)
                        .clipShape(Circle())
                        .overlay(Circle().stroke(Color.black,lineWidth: 5))
                        .scaledToFit()

                }


                    VStack { //For Button
                        Spacer()

                            Button( action: {},label: { Text("Create Order") })

                    }                    
                    .navigationBarTitle(Text("Menu"))


                } //NavView End


        } //VStack End

    }

}
wusliu 回答:Xcode SwiftUI按钮-无法居中

可能不是最优雅的解决方案,但它可以起作用:

HStack() {
     Spacer()
     Button( action: {},label: { Text("Create Order") })
     Spacer()
}
,

第一个快速的主意:在Spacer之间的HStack中设置按钮:

// ...
VStack { //For Button
    Spacer()

    HStack {
        Spacer()
        Button( action: {},label: { Text("Create Order") })
        Spacer()
        }    
}
.navigationBarTitle(Text("Menu"))
// ...

结果是:

enter image description here

,

您可以将.frame(maxWidth: .infinity)添加到Button中。这也将提高教学能力。

Button("Create Order") { }
.frame(maxWidth: .infinity)
本文链接:https://www.f2er.com/3104240.html

大家都在问