SwiftUI NavigationBarItems slideBack freezes app
Asked Answered
C

1

5

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)
            }
        }
    }
}
Cropper answered 15/12, 2019 at 14:16 Comment(7)
Can we see your HomeView?Onshore
I've added HomeView to questionCropper
I don't see anything wrong in your code, unless you have millions of movies. Which Xcode version do you use? Did you perform anything in Details or just navigate forward and backward?Clydesdale
I have Xcode 11.2.1 In Detail I have details of Movie, button with EditingSheet, button which deleting that movie and I can navigate between Home and Detail. When I've put Edit Button as an element of View, it works fine but If I put Edit Sheet Button on NavigationBarItem, it doesn't work - button works, but app freezes sometimesCropper
Also I can't replicate any freezing because I don't know what your AddMovieView() is doing.Onshore
Also you're saying that the issue relates to the editing functionality. Can you provide some more logic around that?Onshore
@Cropper Have you figured this out? I'm running into a semi-similar problem, where essentially, putting custom navigation buttons in the nav bar causes a whole whack of problems.Emeraldemerge
A
3

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.

Amoakuh answered 26/3, 2020 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.