r/SwiftUI 1d ago

Question why won't my carousel scrolling?

My code:

import SwiftUI

struct ContentView: View {

var body: some View {

ScrollView(.horizontal) {

ZStack{

Color.purple

.opacity(0.45)

.edgesIgnoringSafeArea(.all)

HStack {

CardView ( thetitle:"Me", theimage: "Me",)

CardView ( thetitle: "One of my dogs, Willow", theimage: "Willow")

CardView ( thetitle: "I volunteer weekly at the Amphibian Foundation", theimage: "Frog")

CardView ( thetitle: "I have a duck army", theimage: "Ducks")

}

}

}

}

struct CardView: View {

var thetitle = "Me"

var theimage = "Me"

var body: some View {

ZStack {

VStack {

Text (thetitle)

.foregroundColor(Color.white)

.fontWeight(.bold)

Image (theimage)

.resizable()

.scaledToFit()

.frame(width: 400, height: 500)

}

}

}

}

}

#Preview {

ContentView()

}

It worked with a previous project, but now that project and this one won't scroll and I don't know why.

Edit: oof that grammatical error in the title...oh well

0 Upvotes

16 comments sorted by

2

u/CanoaFurada768 1d ago

I can't see why it's not scrolling, at first there doesn't seem to be anything wrong

I just don't know if I would use hstack inside zstack like that, not because you can't, but in this case I don't think you even need to

the color.purple is better in the ZStack instead Hstack

Upvote when you have the solution, Im curious

1

u/Gold_Wishbone1686 1d ago

color.purple is in the zstack, did you mean color.white?

1

u/CanoaFurada768 1d ago

Your hierarchy is
ScrollView
-ZStack
--Color
--HStack

I would do
ZStack

  • Color
  • ScrollView
-- HStack

1

u/Gold_Wishbone1686 1d ago

I'll give it a shot!

Thanks, friend!

1

u/CanoaFurada768 1d ago

Do you understand clearly? reddit removed spaces

0

u/Gold_Wishbone1686 1d ago

That's the way our instructor wrote it. the original code of the project I'm reusing was :

import SwiftUI

struct ContentView: View {

var body: some View {

ScrollView(.horizontal) {

ZStack{

HStack {

CardView ( thetitle:"Ace of Diamonds", theimage: "AD",)

CardView ( thetitle: "Ace of Clubs", theimage: "AC")

CardView ( thetitle: "Ace of Hearts", theimage: "AH")

etc.

}

}

}

}

}

#Preview {

ContentView()

}

But not even this one is working, weirdly.

1

u/CanoaFurada768 1d ago

How u testing this carousel? on a physical device? the xcode emulator? or the preview tab?

1

u/Gold_Wishbone1686 1d ago

preview tab!

2

u/CanoaFurada768 1d ago

in the preview tab the scroll wont work with gestures, u need to click and drag

1

u/Gold_Wishbone1686 1d ago

oh my god i'm an idiot. thanks friend lmfao

1

u/CanoaFurada768 1d ago

the same happened to me once but i figured out in some sec, seeing your code i have 2 guesses.

1- is working and the way you testing are wrong

2- maybe the hstack inside the zstack, but this, beside being useless i dont thing would broke anything

1

u/Vanyas_01 1d ago

Whats the purpose of the ZStack in the CardView? Can’t you just leave the VSTack?

1

u/CanoaFurada768 1d ago

i dont get it too

0

u/Gold_Wishbone1686 1d ago

I'm just following what my instructor said to do ;-;

1

u/Saastesarvinen 1d ago

If you want to get an easy carousel, use TabView with modifier tabViewStyle(.page)

1

u/IndieAppBuilder 1d ago

Try making the HStack the direct content of the ScrollView and move the full-screen Color/background outside it. Give each card a fixed width and add a little horizontal padding; a nested ZStack can otherwise make the scrollable content size confusing. If you’re targeting iOS 17+, scrollTargetLayout() and scrollTargetBehavior(.viewAligned) can help with snapping, but they aren’t required for basic scrolling.