在HStack中对齐图像

问题描述 投票:0回答:1

我在 SwiftUI 中有以下代码:

  HStack(spacing: 0) {
        ForEach(images) { thumbImage in
            Image(uiImage: thumbImage.image)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 50, height: 50)
                .clipped()
        }
    }
    .frame(width: 330, alignment: .center)
    .clipped()

我看到以下输出:

enter image description here

实际上,它正在剪切

HStack
左侧和右侧的内容。我想要的是剪切仅发生在右侧(尾随),而不是左侧。实现这一目标的正确方法是什么?

ios swiftui hstack
1个回答
0
投票

HStack
被剪辑到设置在其上的框架上。默认情况下,
.frame
的对齐方式是
.center
,但您甚至还明确设置了中心对齐方式。

如果您想查看前导内容,但剪切尾随内容,请尝试将对齐方式更改为

.leading
:

HStack(spacing: 0) {
    // ...
}
.frame(width: 330, alignment: .leading) // 👈 HERE
.clipped()
© www.soinside.com 2019 - 2024. All rights reserved.