当前位置:网站首页>一个实际使用SwiftUI 4.0中ViewThatFits自适应视图的例子
一个实际使用SwiftUI 4.0中ViewThatFits自适应视图的例子
2022-07-24 17:00:00 【大熊猫侯佩】

概述
在App图表显示中,我们需要在图表附近画出图表对应的图例。
我们知道,图例的尺寸是和实际占位宽度相匹配的。
如果实际空间比较小,我们就纵向显示图例,否则我们可以利用更大的空间横向显示图例。
一般来说,如何实现上述功能呢?
之前的解决方案
我们可以使用GeometryReader来确定实际宽度,然后据此来改变每个图例的布局:
struct LegendsView: View {
// 判断使用横向或是纵向显示布局
private func layoutByVStack(_ width: CGFloat? = nil) -> Bool {
var layoutByVStack = false
if let width = width, width <= 256 {
layoutByVStack = true
}
return layoutByVStack
}
var body: some View {
GeometryReader {
reader in
HStack {
Text("Hives Count:")
.frame(width: 100, alignment: .leading)
if layoutByVStack(reader.size.width){
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 15, height: 7)
Text("成功")
}
}else{
HStack {
Rectangle()
.fill(Color.blue)
.frame(width: 15, height: 7)
Text("成功")
}
}
if layoutByVStack(reader.size.width){
VStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text("失败")
}
}else{
HStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text("失败")
}
}
if layoutByVStack(reader.size.width){
VStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text("未定义")
}
}else{
HStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text("未定义")
}
}
Spacer()
}
}
}
}
但这样做的话,那个阙值 256 需要多次实验才能最终确定。
SwiftUI 4.0中更灵活的ViewThatFits
SwiftUI 4.0为我们带来了新的ViewThatFits自适应视图,使用它我们不但可以二选一,甚至可以根据布局的紧凑程度从多个备选方案中选择一个最适合的布局:
struct LegendsView2: View {
var body: some View {
HStack {
Text("Hives Count:")
.frame(width: 100, alignment: .leading)
ViewThatFits {
HStack {
HStack {
Rectangle()
.fill(Color.blue)
.frame(width: 15, height: 7)
Text("成功")
}
HStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text("失败")
}
HStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text("未定义")
}
}
HStack {
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 15, height: 7)
Text("成功")
}
VStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text("失败")
}
VStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text("未定义")
}
}
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 15, height: 7)
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
}
}
// 我们还可以用多个ViewThatFits视图,为每个图例实现独立的推荐布局
/* ViewThatFits { HStack { Rectangle() .fill(Color.blue) .frame(width: 15, height: 7) Text("成功") } VStack { Rectangle() .fill(Color.blue) .frame(width: 15, height: 7) Text("成功") } } ViewThatFits { HStack { Rectangle() .fill(Color.orange) .frame(width: 15, height: 7) Text("失败") } VStack { Rectangle() .fill(Color.orange) .frame(width: 15, height: 7) Text("失败") } } ViewThatFits { HStack { Rectangle() .fill(Color.gray.opacity(0.5)) .frame(width: 15, height: 7) Text("未定义") } VStack { Rectangle() .fill(Color.gray.opacity(0.5)) .frame(width: 15, height: 7) Text("未定义") } } */
Spacer()
}
}
}
这样一来,我们可以完全省去GeometryReader和阙值的“烦心事”,非常方便:

总结
在本篇博文中,我们用一个实际的例子介绍了SwiftUI 4.0中ViewThatFits自适应视图的使用,更多内容可以移步以下链接观赏:
SwiftUI 4.0 (iOS 16) 新ViewThatFits视图使用简介
那么,下次再会啦!
边栏推荐
- AMD锐龙7000预计9月15日上市 3D缓存版还要再等等
- At & T pseudo instruction and interpretation of CFI CFA
- 为什么被调函数内部不能用 sizeof(arr) / size(arr[0]) 计算数组长度?
- 期盼已久全平台支持-开源IM项目OpenIM之uniapp更新
- 1184. 公交站间的距离
- regular expression
- Delay and jitter in network
- 1309. Decode letter to integer mapping
- 工信安全中心牵头搭建数据流通平台 蚂蚁集团等厂商提供技术支持
- [technology] chat room demo of uniapp
猜你喜欢

会议OA项目进度(一)

1184. Distance between bus stops

JVM class loading subsystem

Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
![Why can't sizeof (ARR) / size (arr[0]) be used to calculate the array length inside the called function?](/img/e6/1d2213f3eb9bd0ff537a66d2b01f98.png)
Why can't sizeof (ARR) / size (arr[0]) be used to calculate the array length inside the called function?

QT graphical interface beginner project - unmanned aerial vehicle group combat simulation

Custom types: Enumeration

SS-Paper【1】:Fully Convolutional Networks for Semantic Segmentation

我们为什么要推出Getaverse?

15. ARM embedded system: how to debug single board with PC
随机推荐
Topic 6 - message queue for client communication
JS to implement a promise of promises/a+ specification
Axi protocol (3): handshake mechanism and implementation details of Axi architecture
What is fuzzy theory, foundation and process
Picture browser? QT can also be achieved!
Template method mode
MODIS data WGet Download
EF LINQ Miscellany
AXI协议(1):AMBA总线介绍,AXI概念与背景介绍,AXI协议特点与功能
Live review | wonderful playback of Apache pulsar meetup (including PPT download)
SS-Paper【1】:Fully Convolutional Networks for Semantic Segmentation
quick get start
[machine learning basics] - another perspective to explain SVM
EF combined with sqlbulkcopy batch insert data
随笔记:同步、异步和微任务、宏任务的打印顺序
Envi5.3 open GF-1 WFV data
Wechat applet list (list rendering of data rendering)
QML coding specification
[technology] chat room demo of uniapp
Pull and load more on wechat applet list rendering