模态表示问题



我正在尝试从课程视图中的以下代码中获取ContentView((。

.sheet(isPresented: self.$showContent) {ContentView()}

这是完整的代码:

//
//  HomeList.swift
//  Design Code
//
//  Created by Govind Pathak on 09/09/19.
//  Copyright © 2019 Govind Pathak. All rights reserved.
//
import SwiftUI
struct HomeList: View {
@State var showContent = false
var body: some View {
ScrollView(.horizontal , showsIndicators: false){
HStack(spacing:30) {
ForEach(courses) { item in
CourseView(
title: item.title,
image: item.image,
color: item.color,
shadowColor: item.shadowColor)
.sheet(isPresented: self.$showContent) {ContentView()}
}
} .padding(.leading , 30)
}
}
}
struct HomeList_Previews: PreviewProvider {
static var previews: some View {
HomeList()
}
}
struct CourseView: View {
// @Environment(.presentationMode) var presentationMode
var title = "Build an app with SwiftUI"
var image = "Illustration1"
var color = Color("background3")
var shadowColor = Color(red: 0.271, green: 0.235, blue: 0.788, opacity: 0.3)
var body: some View {
VStack(alignment: .leading) {
Text(title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color.white)
.padding(20)
.lineLimit(4)
.padding(.trailing , 50)

Spacer()
Image(image)
.resizable()
.renderingMode(.original)
.aspectRatio(contentMode: .fit)
.frame(width: 246, height: 150)
.padding(.bottom , 30)
}
.background(color)
.cornerRadius(30)
.frame(width: 246, height: 360)
.shadow(color:shadowColor , radius: 20 , x: 0, y: 20)
}
}
struct Course : Identifiable {
var id = UUID()
var title: String
var image: String
var color: Color
var shadowColor: Color
}
let coursesData = [
Course(title: "Build an app with SwiftUI",
image: "Illustration1",
color: Color("background3"),
shadowColor: Color("backgroundShadow3")),
Course(title: "Design Course",
image: "Illustration2",
color: Color("background4"),
shadowColor: Color("backgroundShadow4"))
]
var courses = coursesData

首先,您尝试遍历家庭列表中的课程列表,但课程未在任何地方定义。

这一行:

.sheet(isPresented: self.$showContent) { ContentView() }

应该作为关闭正文声明上的括号之前的最后一件事。

您还需要一种方法将 showContent 值更改为 true。

相关内容

  • 没有找到相关文章

最新更新