SwiftUI调整图片尺寸以适应屏幕尺寸

使用GeometryReader来获取

范例:

1
2
3
4
5
6
7
8
VStack {
GeometryReader { geo in
Image("Example")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: geo.size.width, height: 300)
}
}

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

var body: some View {
GeometryReader { geo in
VStack {
ZStack {
Circle()
.frame(width: geo.size.width / 2, height: geo.size.width / 2)
.foregroundColor(.blue)

Text("?")
.foregroundColor(.white)
.font(.system(size: 60, weight: .bold, design: .default))

}

ForEach(0 ..< 3) { item in
Circle()
.frame(width: geo.size.width / 4, height: geo.size.width / 4)
}
}
}

}