SwiftUI NavigationBarItems slideBack freezes app

自作多情 提交于 2020-08-07 06:41:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!