这是我的故障视图代码:
import SwiftUI
import CoreData
struct DetailMovieView: View {
let movie: Movie
@Environment(\.managedObjectContext) var moc
@Environment(\.presentationMode) var presentationMode
@State private var showingDeleteAlert = false
@State private var showingEditScreen = false
var body: some View {
GeometryReader { geometry in
VStack {
ScrollView {
HStack {
Text(self.movie.title ?? "Unknown Movie")
.font(.largeTitle)
.multilineTextAlignment(.center)
Button(action: {
self.showingDeleteAlert = true
}) {
Image(systemName: "trash")
}
}
ZStack(alignment: .bottomTrailing) {
Image(self.movie.genre ?? "Fantasy")
.renderingMode(.original)
.resizable()
.frame(width: geometry.size.width, height: 250.0)
.clipShape(Rectangle())
.overlay(Rectangle().stroke(Color.black, lineWidth: 2))
.shadow(radius: 10)
Text(self.movie.genre?.uppercased() ?? "FANTASY")
.font(.caption)
.fontWeight(.black)
.padding(8)
.foregroundColor(.white)
.background(Color.black.opacity(0.75))
.clipShape(Capsule())
.offset(x: -5, y: -5)
}
Text("Director: 2\(self.isThereDirector(director: self.movie.director ?? "Unknown director"))")
.font(.caption)
.foregroundColor(.secondary)
Text("Year of Production: \(self.YearOfProduction(year: self.movie.productionYear))")
.font(.caption)
.foregroundColor(.secondary)
Text(self.movie.review ?? "No review")
.padding()
RatingView(rating: .constant(Int(self.movie.rating)))
.font(.largeTitle)
Spacer()
}
}
.alert(isPresented: self.$showingDeleteAlert) {
Alert(title: Text("Delete Movie"), message: Text("Are you sure?"), primaryButton: .destructive(Text("Delete")) {
self.deleteMovie()
}, secondaryButton: .cancel()
)
}
}
.navigationBarItems(trailing: Button(action: {
self.showingEditScreen.toggle()
}) {
Image(systemName: "pencil")
.imageScale(.large)
.accessibility(label: Text("Edit Movie"))
.padding()
})
.sheet(isPresented: $showingEditScreen) {
EditMovieView(movie: self.movie)
}
}
func deleteMovie() {
moc.delete(movie)
presentationMode.wrappedValue.dismiss()
}
func YearOfProduction(year: Int16) -> String {
if Int(year) != 0 {
return String(year)
}
else {
return "Unknown"
}
}
func isThereDirector(director: String) -> String {
if director != "" {
return director
}
else {
return "Unknown"
}
}
}
struct DetailMovieView_Previews: PreviewProvider {
static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
static var previews: some View {
let movie = Movie(context: moc)
movie.title = "Test Movie"
movie.director = "Test director"
movie.genre = "Fantasy"
movie.rating = 4
movie.review = "Good movie"
movie.productionYear = 2019
return NavigationView {
DetailMovieView(movie: movie)
}
}
}
我回到上一个NavigationView时遇到问题。当我想向后滑动或单击navigationView GoingBackButton时,可以做到,但是当我不完全向后滑动时,例如当我...
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("Show Detail View")
}.navigationBarTitle("Navigation")
}
}
}