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.2 Answers
Reset to default 0If 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
版权声明:本文标题:swiftui - How to set HStack child alignment different - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736641438a1946003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论