SwiftUI alignment Images

Hello,


I am looking for a solution to center my pictures in the ZStack. This on both the x and y axes. I want the image to fit into the stack, adjust to the width and just be displayed completely in the middle. What does not fit should be "cut off". ".aspectRatio (contentMode: .fill)" does not help me. ".stretch" adapts it to the width, but squeezes it into the display. Thank you for your help.


Greetings.

Replies

I didn't try it, but I thought of this:

- embed the image in a view with the width of the stack

- center the image in this view.

I´m a beginner and in my mind i would like to keep the code so flexible as possible. My simple app is only intended for the iPad actualy, but for the future: I don´t know what for a device the user will use. So i thought, i could keep it more flexible and tell him not a explicit width.

Sure, what I meant is to make sure the width of the subview is equal to the stack width, dynamically.


Note: you should move your question to SwiftUI section of the forum.

I can´t remove it into SwiftUI. Because there is no SwiftUI to select.


Sorry if i maybe misunderstand you: My stack width is perfect. But the image in this stack isn´t centered on the x and y axes. It´s not exactly the middle of the image. That´s my problem.


Please have a look:

https://ibb.co/6XTtSzp

https://ibb.co/fd7jdF0

https://ibb.co/JcNt6Rz

https://ibb.co/TR2GLc1


ZStack (alignment: .leading) {
            
            Image(internImage.imageName)
                .resizable(resizingMode: .tile)
                //.aspectRatio(contentMode: .fill)
                .clipped()
            
        }.background(Color.white)
            .clipped()
            .cornerRadius(8, antialiased: true)
            
            .frame(height: 155)
        
    }