1
votes

Bouton Xcode SwiftUI - Impossible de centrer

Je suis nouveau sur SwiftUI et je voudrais de l'aide concernant le positionnement d'un bouton personnalisé que je ne parviens pas à centrer. J'ai utilisé VStack et HStack pour essayer de centrer le bouton en bas, mais le bouton continue de s'aligner à gauche. Toute aide sera appréciée.

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

    }

}


0 commentaires

3 Réponses :


2
votes

Ce n'est peut-être pas la solution la plus élégante mais cela fonctionne:

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


0 commentaires

0
votes

Première idée rapide: placez votre bouton dans HStack entre Spacers:

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

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

et le résultat est:

 entrez la description de l'image ici


0 commentaires

3
votes

Vous pouvez ajouter .frame (maxWidth: .infinity) au Button . Cela augmentera également la capacité d'apprentissage.

Button("Create Order") { }
.frame(maxWidth: .infinity)


0 commentaires