admin管理员组

文章数量:1344146

Okay, so I have a Horizontal ScrollView that I cant access because the images I have are vertical ones that are pretty big in definition. Therefore even if I give it a frame, it overflows over the Horizontal ScrollView and I cannot access the scrollview anymore, but only when I use scaledToFill(). If I use scaledToFit(), it works. What is going on?

This is my ContentView

Categories()
                
ForEach(images, id: \.self) { image in
    Card(image: image)
}

This is my CardView

Image(image)
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(height: 300)
    .frame(maxWidth: .infinity)
    .clipped()

This is my CategoriesView

ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 10) {
                ForEach(items.indices, id: \.self) { index in
                    Button {
                        selected = index
                    } label: {
                        Text(items[index])
                            .padding(.horizontal)
                            .padding(.vertical, 12)
                            .background(
                                selected == index ? AnyShapeStyle(.cyan) : AnyShapeStyle(.ultraThinMaterial))
                            .clipShape(RoundedRectangle(cornerRadius: 10))
                    }
                    .buttonStyle(.plain)
                }
            }
            .padding(.horizontal)
        }

Okay, so I have a Horizontal ScrollView that I cant access because the images I have are vertical ones that are pretty big in definition. Therefore even if I give it a frame, it overflows over the Horizontal ScrollView and I cannot access the scrollview anymore, but only when I use scaledToFill(). If I use scaledToFit(), it works. What is going on?

This is my ContentView

Categories()
                
ForEach(images, id: \.self) { image in
    Card(image: image)
}

This is my CardView

Image(image)
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(height: 300)
    .frame(maxWidth: .infinity)
    .clipped()

This is my CategoriesView

ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 10) {
                ForEach(items.indices, id: \.self) { index in
                    Button {
                        selected = index
                    } label: {
                        Text(items[index])
                            .padding(.horizontal)
                            .padding(.vertical, 12)
                            .background(
                                selected == index ? AnyShapeStyle(.cyan) : AnyShapeStyle(.ultraThinMaterial))
                            .clipShape(RoundedRectangle(cornerRadius: 10))
                    }
                    .buttonStyle(.plain)
                }
            }
            .padding(.horizontal)
        }
Share Improve this question asked 8 hours ago Aleck David HollyAleck David Holly 11 bronze badge New contributor Aleck David Holly is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 0
Add a comment  | 

1 Answer 1

Reset to default 0

The answer was to add a .contentShape() with the shape of my choice to the image View for it to not overflow and allowing me to still access the scrollView.

本文标签: swiftEven with clipped()scaledToFill() overflows in SwiftUIStack Overflow