I’m a newbie in SwiftUI and I want to create this card on the screen
I wrote this code snippet:
struct StakingCard: View {
var body: some View {
ZStack(alignment: .leading){
VStack(alignment: .leading){
Text("Staking CAKE").font(.headline)
.foregroundColor(.white)
Text("Stake tokens and get rewards")
.foregroundColor(.white)
}.frame(maxWidth: .infinity, maxHeight: 100)
.background(
Image("blac")
.resizable()
.scaledToFill())
.padding(0)
}.cornerRadius(10)
.padding(.horizontal,10)
}
}
But when I insert it into my Content view,
I get the following Screen:
The padding in the card is resizing and it does’not look good
Kindly direct me on how to go about this, thanks.
2
Answers
It may be helpful to share the code for the whole view of the screen, and not just the individual component since there are many factors that could be causing the issue.
Most likely, the fix would be to set a fixed width on the view instead of using
maxWidth: .infinity
. Because the view is inside of a container that is scrolling, it’s impossible to figure out what the maximum width might be.A couple notes:
.padding
modifier on them (only.padding(0)
on the image and.padding(10)
on theZStack
).padding(10)
is the padding betweenStakingCard
‘s – that should be added by theHStack
the cards exist in, not the card itself.ZStack
– theImage
is set as the background via the.background
modifier.maxWidth: .infinity
isn’t necessary, the cells are in a scrolling collection with unbounded width.Here’s an updated version: