【发布时间】:2019-07-02 06:59:41
【问题描述】:
我想在视图中显示图像,其中图像源的大小会有所不同。 有没有办法将图像的大小动态调整为包含视图的宽度,或者甚至获得屏幕大小? 我有一个水平滚动视图,每个元素都是图像和文本的堆栈。我想根据屏幕宽度调整每个图像的大小,因为我想独立于设备。 你会如何在 SwiftUI 中做到这一点? 谢谢!
struct ImageRow: View {
var items: [ImageWithDescription]
var body: some View {
ScrollView(showsHorizontalIndicator: false) {
HStack(alignment: .bottom){
ForEach(self.items.identified(by: \.name)) { item in
Spacer()
VStack {
Image(uiImage: item.image ?? UIImage())
.resizable()
.frame(minWidth: (item.image.size.width) / 3, height: (item.image.size.height) / 3)
.cornerRadius(20, antialiased: true)
.clipShape(Rectangle())
.shadow(radius: 10)
Text(item.name)
.color(.primary)
.font(.subheadline)
}
}
.padding()
}
}
.frame(height: 300)
}
}
【问题讨论】: