问题
My HomeView
(where I store list of Movies
) has NavigationView
and NavigationLink
with destination to DetailView
.
When I want to add NavigationBarItems
in my DetailView
, it makes my GoBack Slide (from DetailView
to HomeView
) useless. The app freezes when I stop sliding in ~1/3 of screen.
I don't have additional NavigationView
in DetailView
, because when I had it I had it doubled in DetailView
.
I found lines of code which ruins everything.
It's part with NavigationBarItems
:
.navigationBarItems(trailing: Button(action: {
self.showingEditScreen.toggle()
}) {
Image(systemName: "pencil")
.imageScale(.large)
.accessibility(label: Text("Edit Movie"))
.padding()
})
And HomeView
:
struct HomeView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Movie.entity(), sortDescriptors: [
NSSortDescriptor(keyPath: \Movie.title, ascending: true),
NSSortDescriptor(keyPath: \Movie.director, ascending: true)
]) var movies: FetchedResults<Movie>
@State private var showingAddScreen = false
func deleteMovie(at offsets: IndexSet) {
for offset in offsets {
let movie = movies[offset]
moc.delete(movie)
}
try? moc.save()
}
var body: some View {
NavigationView {
List {
ForEach(movies, id: \.self) { movie in
NavigationLink(destination: DetailMovieView(movie: movie)) {
EmojiRatingView(rating: movie.rating)
.font(.largeTitle)
VStack(alignment: .leading) {
Text(movie.title ?? "Unknown Title")
.font(.headline)
Text(movie.director ?? "Unknown Director")
.foregroundColor(.secondary)
}
}
}
.onDelete(perform: deleteMovie)
}
.navigationBarTitle("Movie Store")
.navigationBarItems(leading: EditButton(), trailing: Button(action: {
self.showingAddScreen.toggle()
}) {
Image(systemName: "plus")
.imageScale(.large)
//.accessibility(label: Text("Add Movie"))
.padding()
})
.sheet(isPresented: $showingAddScreen) {
AddMovieView().environment(\.managedObjectContext, self.moc)
}
}
}
}
回答1:
This is currently a bug with SwiftUI. If you have a .sheet
and/or a .alert
in your DetailView
, remove them and it will work as expected, without the app freezing.
来源:https://stackoverflow.com/questions/59344756/swiftui-navigationbaritems-slideback-freezes-app