Search code examples
swiftscrollviewswiftui

Stop vertical scroll in SwiftUI ScrollView


I have tried to implement a scrollView using swiftUI. Inside the scrollview there is a horizontal stack(HStack) containing another view. I wanted the scrollview to scroll horizontally. But it also scrolls vertically. Following is my code:

struct categoryRow : View {
var categoryName: String
var landmarks : [Landmark]
var body: some View {
    VStack(alignment: .leading){
        Text(categoryName).font(.headline).padding(EdgeInsets(top: 15, leading: 10, bottom: 0, trailing: 0))
        ScrollView (alwaysBounceHorizontal: true, showsHorizontalIndicator: false, showsVerticalIndicator: false ){
            HStack(spacing: 15){
                ForEach(landmarks) { landmark in
                    NavigationButton(destination: LandMarkDetails(landmark: landmark)) {
                        CategoryItem(landmark: landmark)
                    }
                }
            }.padding()
        }.frame( height: 190)
    }
}
}

I could not find any option yet to stop the vertical scrolling. What am I missing here?


Solution

  • If ScrollView's height is less than the content's height it will scroll vertically too.

    Just make it higher or the content narrower.