admin管理员组

文章数量:1125946

I have HStack with two views: Image and Text. I would like to align Image top but the text center on VerticalAlignment. However, specifying alignment .center for the Text does not do the trick , it is still positioned top. Is there anything I am missing?

struct SwiftUIView: View {
    var body: some View {
        HStack(alignment: .top) {
            Image(systemName: "star.fill")
                .resizable()
                .frame(width: 100, height: 100)
                .background(.green)

            HStack(alignment: .center) {
                Text("Text that has dynamic height and should be aligned center")
                    .background(.pink)
            }
        }
        .background(.gray)
    }
}

Preview screenshot

I have HStack with two views: Image and Text. I would like to align Image top but the text center on VerticalAlignment. However, specifying alignment .center for the Text does not do the trick , it is still positioned top. Is there anything I am missing?

struct SwiftUIView: View {
    var body: some View {
        HStack(alignment: .top) {
            Image(systemName: "star.fill")
                .resizable()
                .frame(width: 100, height: 100)
                .background(.green)

            HStack(alignment: .center) {
                Text("Text that has dynamic height and should be aligned center")
                    .background(.pink)
            }
        }
        .background(.gray)
    }
}

Preview screenshot

Share Improve this question edited 2 days ago SilKirss asked 2 days ago SilKirssSilKirss 32 bronze badges New contributor SilKirss is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 0

If I understand correctly, you want the image to be aligned to the top of the HStack when the text is taller than the image, and you want the text to be centered vertically when the text is shorter than the image.

Instead of wrapping the text in another HStack, you should add a frame around the text.

Text("Text that has dynamic height and should be aligned center")
    .background(.pink)
    .frame(minHeight: 100, alignment: .center)

This (invisible) frame should be at least as tall as the image (100pt), and the text should have .center alignment inside that frame.

.center is the default value for alignment:, so writing .frame(minHeight: 100) suffices. I included it just so it is clear what is going on.


When the text is shorter than the image:

When the text is taller than the image:

If you know the size of the image then one way to align the text is to apply an .alignmentGuide. The nested HStack is not needed:

let starSize: CGFloat = 100

var body: some View {
    HStack(alignment: .top) {
        Image(systemName: "star.fill")
            .resizable()
            .frame(width: starSize, height: starSize)
            .background(.green)

        Text("Text that has dynamic height and should be aligned center")
            .background(.pink)
            .alignmentGuide(.top) { dimensions in
                min(0, (dimensions.height - starSize) / 2)
            }
    }
    .background(.gray)
}

本文标签: swiftuiHow to set HStack child alignment differentStack Overflow