如何在视图中的单个VStack中添加多个按钮(Xcode12,SwiftUI)



我在单个VStack中有多个按钮时遇到问题。我得到错误:额外的参数在位置。。。下面是我的代码。此代码在视图文件夹中,我正在使用带有SwiftUI 的Xcode12

确切的错误消息:调用中#11、#12、#13位置的额外参数

import SwiftUI
struct HomeView: View {

@Binding var page: Pages

var drink = Drink.AllDrinks

var body: some View {

VStack {
drink[0].image
.resizable()
.frame(width:80, height: 80)
Text(drink[0].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
Spacer()
.frame(height: 35)

drink[1].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[1].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}

drink[2].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[2].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}

drink[7].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[7].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}


}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView(page: .constant(Pages.Home))
}
}
struct OrderButtonContent: View {
var body: some View {
Text("Order")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 80, height: 30)
.background(Color.blue)
.cornerRadius(15.0)
}
}

这很容易,让我们看看错误是什么:Exact error message: Extra arguments at positions #11, #12, #13 in call

它说额外的参数!SwiftUI中参数的最大数量为10,我们应该使用Group,如下所示:

struct HomeView: View {

@Binding var page: Pages

var drink = Drink.AllDrinks

var body: some View {

VStack {

Group {   // <<: Here!

drink[0].image
.resizable()
.frame(width:80, height: 80)

Text(drink[0].name)

Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
Spacer()

.frame(height: 35)

drink[1].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[1].name)

Button(action: {page = Pages.Home}) {
OrderButtonContent()
}

}

Group {   // <<: Here!

drink[2].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[2].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}

drink[7].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[7].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}

}



}
}
}

相关内容

  • 没有找到相关文章

最新更新