简体   繁体   English

如何将文本与有框架的 VStack 底部对齐?

[英]How do i align text to the bottom of a VStack that has a frame?

I am creating a view that is a rectangle that contains an: image, name, subtitle.我正在创建一个包含以下内容的矩形视图:图像、名称、字幕。

I have these in a VStack with a frame.我将这些放在带有框架的 VStack 中。 In this VStack, the views stack correctly.在此 VStack 中,视图正确堆叠。 However, I want the subtitle (which is text) to always be stuck to the bottom of the VStack.但是,我希望字幕(即文本)始终贴在 VStack 的底部。

At the moment the subtitle is pushed up with the other views.目前,字幕与其他视图一起向上推。 I tried using Spacer's but these didn't seem to do the job (maybe I used them wrong???).我尝试使用 Spacer,但这些似乎没有完成工作(也许我用错了???)。

Here is the view:这是视图:

   VStack(alignment: .center, spacing: 0) {
                    
                    
                    Image("Image")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .clipped()
                                .padding()
                                .layoutPriority(1)
                        }
                    }
                                         
                    
                    Text("Name")
                    .font(.title2)
                    .fontWeight(.bold)
                    .foregroundColor(.black)
                    .minimumScaleFactor(0.5)
                    .multilineTextAlignment(.center)
                    .padding(.leading, 5)
                    .padding(.trailing, 5)
                    .padding(.bottom, 5)
                    

                    Text("Subtitle")
                    .font(.caption)
                    .foregroundColor(.gray)
                    .lineLimit(2)
                    .padding(1)
          
            }
                .frame(width: 150, height: 200)

Any ideas?有任何想法吗?

Put the subtitle into an .overlay() of the VStack and use alignment: .bottom to push it to the bottom:将字幕放入 VStack 的VStack .overlay()并使用alignment: .bottom将其推到底部:

VStack(alignment: .center, spacing: 0) {
    
    Image("Image")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .clipped()
        .padding()
        .layoutPriority(1)

    Text("Name")
        .font(.title2)
        .fontWeight(.bold)
        .foregroundColor(.black)
        .minimumScaleFactor(0.5)
        .multilineTextAlignment(.center)
        .padding(.leading, 5)
        .padding(.trailing, 5)
        .padding(.bottom, 5)
}
.frame(width: 150, height: 200)
.overlay(alignment: .bottom) {
    Text("Subtitle")
        .font(.caption)
        .foregroundColor(.gray)
        .lineLimit(2)
        .padding(1)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM