How can I position 1 image relative to another in a ZStack in SwiftUI?

626 Views Asked by At

I have 2 images in a ZStack. How can I position image "x.circle" to the top right of the image "person.fill"?

ZStack {
        
        Image(systemName: "person.fill")
            .font(.system(size: 200))
        
        Image(systemName: "x.circle")
            .font(.system(size: 20, weight: .bold))
            .foregroundColor(.red)
            .background(.white)
            .clipShape(Circle())
}
1

There are 1 best solutions below

0
ChrisR On BEST ANSWER

Use ZStack(alignment: .topTrailing:

    ZStack(alignment: .topTrailing) {   // here
                
        Image(systemName: "person.fill")
            .font(.system(size: 200))
                
        Image(systemName: "x.circle")
            .font(.system(size: 20, weight: .bold))
            .foregroundColor(.red)
            .background(.white)
            .clipShape(Circle())
    }