我正在 SwiftUI 中构建一个食谱应用程序。我创建了主屏幕,其中包含我在单独文件中创建的几个视图列表。(见图片,我还没有所有的图像,所以我对所有视图都有相同的图像)
我想将视图嵌入到 anavigationLink
中,以便当用户点击 A 菜时,它将转到详细信息屏幕。这是我的代码:
struct MenuTopicView: View {
var titleText: String
var foodImageName: String
var body: some View {
NavigationLink(destination: RecipeView(recipeName: titleText)) {
ZStack {
Image(foodImageName)
.resizable()
VStack {
Text(titleText)
.foregroundColor(Color.init(hex: "AAAAAA"))
.font(.system(size: 30, weight: .semibold))
Spacer()
}
}
.frame(width: 189, height: 194)
.cornerRadius(15)
.shadow(color: Color.init(hex:"000000"), radius: 7,x: 7, y: 7)
}
}
}
但是当我运行它时,我得到了这个:
这是主视图的代码,我在其中创建了该布局:
struct ContentView: View {
private var spacing: CGFloat = 20
var body: some View {
VStack {
Text("First Course")
.frame(height: 36)
.font(.system(size: 25, weight: .semibold))
ScrollView (Axis.Set.horizontal, showsIndicators: false){
HStack{
MenuTopicView(titleText: "Soup", foodImageName: "Soup")
MenuTopicView(titleText: "Fish", foodImageName: "Soup")
}
}.frame(height: 200)
Text("Main Course")
.frame(height: 36)
.font(.system(size: 25, weight: .semibold))
ScrollView(Axis.Set.horizontal) {
HStack {
MenuTopicView(titleText: "Steak", foodImageName: "Soup")
MenuTopicView(titleText: "Chicken", foodImageName: "Soup")
}
}
Text("Dessert")
.frame(height: 36)
.font(.system(size: 25, weight: .semibold))
ScrollView(Axis.Set.horizontal) {
HStack {
MenuTopicView(titleText: "Ice cream", foodImageName: "Soup")
MenuTopicView(titleText: "Pancakes", foodImageName: "Soup")
}
}
}
}
}
我用谷歌搜索了一段时间,但我没有成功得到这个问题的答案。有人有什么建议吗?
谢谢!
BSM