Navigation Link doesn’t work with ScrollView inside a List in Swift UI

I am trying to achieve Horizontal scroll of a scrollview inside a List. Which is working fine.

However, if I am adding NavigationLink to each ScrollView Element, it seems like NavigationView is unable to do his task.

I have added my code. Please have a look and let me know, what I am missing.

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

This Post Has One Comment

  1. No Fault

    I replace the Views with Text(“1”) and found everything was fine. So it must be something wrong inside your either/both of your views.

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

Leave a Reply