Hey, y’all, I was trying to follow a tutorial by iOS Academy on YouTube to create a pop-up view, however, I get an error from Swift Playgrounds, saying that I need to put .self and set it as! Content.type. But the tutorial didn’t add these in but still got a perfect result. If I add these in, I get an error that abort() called. Please help me.
Code:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var menu: [MenuModel] = menuData
@State var cardShown = false
@State var cardDismiss = false
let layout = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
var body: some View {
ZStack {
List {
LazyVGrid(columns: layout, spacing: 20) {
ForEach(menu, id: \.self) { item in
Button(action : {
cardShown.toggle()
cardDismiss.toggle()
}, label : {
MenuTile(menu: item)
})
BottomCardView(cardShown: $cardShown, cardDismiss: $cardDismiss) {
CardContent(menu: item)
.padding()
}
}
}
}
}
}
}
struct CardContent: View {
var menu: MenuModel
var body: some View {
Image(uiImage: menu.image)
.resizable()
.scaledToFit()
.frame(width: 150, height: 150)
Text(menu.title)
.font(.title)
.fontWeight(.bold)
Text(menu.headline)
.font(.headline)
.fontWeight(.semibold)
Text(menu.description)
.multilineTextAlignment(.center)
GroupBox(){
HStack {
Text("Price")
.padding(.horizontal,25)
Text(menu.price)
}
}
GroupBox(){
HStack {
Text("Nutrition")
.padding(.horizontal,25)
Text(menu.nutrition)
}
}
}
}
struct BottomCardView<Content: View>: View {
var content = Content.self
@Binding var cardShown: Bool
@Binding var cardDismiss: Bool
init(
cardShown: Binding<Bool>,
cardDismiss: Binding<Bool>,
@ViewBuilder content: () -> Content) {
_cardShown = cardShown
_cardDismiss = cardDismiss
self.content = content() as! Content.Type
}
var body: some View {
ZStack {
GeometryReader() { _ in
EmptyView()
}
.background(Color.red.opacity(0.3))
.opacity(cardShown ? 1 : 0)
.animation(Animation.easeIn)
.onTapGesture {
cardShown.toggle()
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct MenuTile: View {
var menu: MenuModel
var body: some View {
VStack {
Image(uiImage: menu.image)
.resizable()
.scaledToFit()
.frame(width: 100, height: 100)
Text(menu.title)
.font(.headline)
.padding(.top)
.multilineTextAlignment(.center)
}
.frame(width: 125, height: 250)
}
}
PlaygroundPage.current.setLiveView(ContentView(menu: menuData))
Tutorial: SwiftUI: Bottom Card / Sheet (2021, Xcode 12, SwiftUI 2.0) - iOS Development - YouTube