Wednesday, October 28, 2026
HomeiOS Developmentios - SwiftUI unusual behaviour with HStack

ios – SwiftUI unusual behaviour with HStack

[ad_1]

As title says I can’t perceive the place is the issue in my format.

I created a easy view known as SmallCard

struct SmallCard: View {
    var picture: Picture
    var title: String
    
    var physique: some View {
        VStack(spacing: 0.0) {
            self.picture
                .resizable()
                .scaledToFit()
            
            ZStack {
                Shade.white
                
                VStack(alignment: .main) {
                    Textual content(self.title)
                        .padding([.vertical])
                }
                
            }
        }
        .rounded(radius: 10)
        .shadow(radius: 2.0)
    }
}

struct SmallExperienceCard_Previews: PreviewProvider {
    static var previews: some View {
        SmallExperienceCard(picture: Picture("img02"), title: "Card title")
    }
}

with this preview

Then I created one other view known as TestExploreCards

struct TestExploreCards: View {
    var physique: some View {
        ScrollView {
            HStack(alignment: .middle, spacing: 8.0) {
                SmallExperienceCard(picture: Picture("img04"),
                                    title: "Card title 01")
                
                SmallExperienceCard(picture: Picture("img05"),
                                    title: "Card title 03")
            }
        }
    }
}

struct TestExploreCards_Previews: PreviewProvider {
    static var previews: some View {
        TestExploreCards()
    }
}

the place I put two SmallCard to get this result however as an alternative I get this.

As you’ll be able to see within the SmallCard preview Picture is identical width because the view beneath, however once I put in a HStack with one other occasion of SmallCard it is not. I am unable to determine the place is the error :

Simply to be clear, rounded is a View extension

extension View {
    func rounded(radius: CGFloat = 25.0) -> some View {
        self.clipShape(RoundedRectangle(cornerRadius: radius, fashion: .steady))
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments