我试图实现列表内的滚动视图的水平滚动。哪个工作正常。

但是,如果我向每个ScrollView元素添加NavigationLink,似乎NavigationView无法完成其任务。

我已经添加了代码。请看看,让我知道,我所缺少的。

struct MovieHomeView: View {
    @ObservedObject var moviesDataSource: MoviesHomeViewModel = MoviesHomeViewModel()

    var body: some View {
        NavigationView {
            VStack {
                HeaderView()
                    .frame(height: 50.0)

                List {
                    MovieCategoryCell(category: "Now Playing", movies: moviesDataSource.nowPlayingMovies)
                    MovieCategoryCell(category: "Popular", movies: moviesDataSource.popularMovies)
                }
                .padding(.horizontal, -20)
            }
            .navigationBarTitle("Movies App")
            .navigationBarHidden(true)
        }
    }
}


struct MovieCategoryCell: View {
    var category: String
    var movies:  [MovieBaseProtocol]

    init(category: String, movies: [MovieBaseProtocol]) {
        self.category = category
        self.movies = movies
    }

    var body: some View {
        VStack(alignment: .leading) {
            Text(category)
                .font(.title)
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    ForEach(self.movies, id: \.id) { movie in
                        NavigationLink(destination: MovieUIView(movie: movie)) {
                            MovieCard(movie: movie)
                        }
                    }
                }
            }
            .frame(height: 280)
        }
        .padding(.horizontal, 20)
    }
}

最佳答案

我用Text(“ 1”)替换了Views,发现一切都很好。因此,在您的一个或两个视图中都一定有问题。

    ForEach(self.movies, id: \.id) { movie in
                    NavigationLink(destination: Text("1")){// MovieUIView(movie: movie)) {
                     Text("1") //   MovieCard(movie: movie)
                    }
                }

关于ios - NavigationLink无法在SwiftUI的列表中与ScrollView一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58678869/

10-10 20:54