I am new to SwiftUI. I am trying to create a view like heart card but not being able to place the vStack of heart and letter at the top-right(topTrailing) corner of the rectangle.
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 5.0)
.fill(.white)
.stroke(.gray, style: StrokeStyle(lineWidth: 2.0))
.frame(width: 100, height: 150)
.shadow(color: Color(.black).opacity(0.3), radius: 5)
VStack(spacing: 0) {
Text("A")
.font(.callout)
Image(systemName: "heart.fill")
.resizable()
.frame(width: 10, height: 10)
}.frame(alignment: .topTrailing)
}
}
Can someone please help.


If you add a border to the
VStack, you can see how the content of theVStackcompletely fills its frame:What this
.framemodifier is doing is to align the content of theVStackwithin a frame of the same size. But since the content already fills the frame, it makes no difference.With these changes it can be made to work:
Move the
.framemodifier that sets the size of the card from theRoundedRectangleto theZStackthat contains all the content.Add
maxWidth: .infinity, maxHeight: .infinityto the.framemodifier being set on theVStack, so that the frame expands to use all of the space available.You might like to add a bit of padding to the
VStacktoo.So what this does is to fix the size of the
ZStackto the card size. ARoundedRectangle(like anyShape) is greedy, so it expands to fill the same space. The.framemodifier on theVStacknow causes it to expand to the size of theZStackand thealignmentparameter aligns the content as you intended it to.VStack: