slideBack冻结应用程序

slideBack冻结应用程序

本文介绍了SwiftUI NavigationBarItems slideBack冻结应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HomeView(我在其中存储Movies的列表)具有NavigationViewNavigationLink,目标位置为DetailView.

My HomeView (where I store list of Movies) has NavigationView and NavigationLink with destination to DetailView.

当我想在DetailView中添加NavigationBarItems时,它会使我的 GoBack幻灯片(从DetailViewHomeView)失效.当我停止在屏幕的约1/3处滑动时,该应用程序冻结.

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.

我在DetailView中没有其他的NavigationView,因为当我拥有它时,我在DetailView中将其加倍.

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.

这是NavigationBarItems的一部分:

.navigationBarItems(trailing: Button(action: {
    self.showingEditScreen.toggle()
}) {
    Image(systemName: "pencil")
    .imageScale(.large)
    .accessibility(label: Text("Edit Movie"))
    .padding()
})

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)
            }
        }
    }
}

推荐答案

当前是SwiftUI的错误.如果DetailView中有.sheet和/或.alert,请将它们删除,即可正常使用,而不会冻结应用程序.

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.

这篇关于SwiftUI NavigationBarItems slideBack冻结应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:10