当前位置:网站首页>An example of using viewthatfits adaptive view in swiftui 4.0
An example of using viewthatfits adaptive view in swiftui 4.0
2022-07-24 17:05:00 【Giant panda Hou Pei】

summary
stay App Chart display in progress , We need to draw the legend corresponding to the chart near the chart .
We know , The size of the legend matches the actual footprint .
If the actual space is relatively small , We will display the legend vertically , Otherwise, we can use a larger space to display the legend horizontally .
Generally speaking , How to realize the above functions ?
Previous solutions
We can use GeometryReader To determine the actual width , Then change the layout of each legend accordingly :
struct LegendsView: View {
// Decide whether to use horizontal or vertical display layout
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(" success ")
}
}else{
HStack {
Rectangle()
.fill(Color.blue)
.frame(width: 15, height: 7)
Text(" success ")
}
}
if layoutByVStack(reader.size.width){
VStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text(" Failure ")
}
}else{
HStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text(" Failure ")
}
}
if layoutByVStack(reader.size.width){
VStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text(" Undefined ")
}
}else{
HStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text(" Undefined ")
}
}
Spacer()
}
}
}
}
But if you do that , That threshold 256 It takes many experiments to finally determine .
SwiftUI 4.0 More flexible ViewThatFits
SwiftUI 4.0 It brings us new ViewThatFits Adaptive view , Using it, we can not only choose one of two , You can even choose the most suitable layout from multiple alternatives according to the compactness of the layout :
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(" success ")
}
HStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text(" Failure ")
}
HStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text(" Undefined ")
}
}
HStack {
VStack {
Rectangle()
.fill(Color.blue)
.frame(width: 15, height: 7)
Text(" success ")
}
VStack {
Rectangle()
.fill(Color.orange)
.frame(width: 15, height: 7)
Text(" Failure ")
}
VStack {
Rectangle()
.fill(Color.gray.opacity(0.5))
.frame(width: 15, height: 7)
Text(" Undefined ")
}
}
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)
}
}
// We can also use multiple ViewThatFits View , Implement an independent recommended layout for each legend
/* ViewThatFits { HStack { Rectangle() .fill(Color.blue) .frame(width: 15, height: 7) Text(" success ") } VStack { Rectangle() .fill(Color.blue) .frame(width: 15, height: 7) Text(" success ") } } ViewThatFits { HStack { Rectangle() .fill(Color.orange) .frame(width: 15, height: 7) Text(" Failure ") } VStack { Rectangle() .fill(Color.orange) .frame(width: 15, height: 7) Text(" Failure ") } } ViewThatFits { HStack { Rectangle() .fill(Color.gray.opacity(0.5)) .frame(width: 15, height: 7) Text(" Undefined ") } VStack { Rectangle() .fill(Color.gray.opacity(0.5)) .frame(width: 15, height: 7) Text(" Undefined ") } } */
Spacer()
}
}
}
thus , We can completely eliminate GeometryReader And the threshold “ Upset ”, Very convenient :

summary
In this blog post , We use a practical example to introduce SwiftUI 4.0 in ViewThatFits Use of adaptive views , More content can be viewed from the following links :
SwiftUI 4.0 (iOS 16) new ViewThatFits Introduction to view usage
that , See you next time !
边栏推荐
- Development dynamics | stonedb 2022 release milestone
- Qsqldatabase: solution of qmmysql driver not loaded
- Logisim group experiment 10 single cycle MIPS CPU
- Small end format and big end format (little endian & big endian)
- QT graphical interface beginner project - unmanned aerial vehicle group combat simulation
- Getting started with arcpy
- Programming language exercises (I)
- PAT甲级——拼写正确
- JSP custom tag library --foreach
- Parental delegation mechanism
猜你喜欢

Logisim group experiment 10 single cycle MIPS CPU

Mysql增删改查、检索与约束(详细教学)

查数据库实际数据增长情况

Programming language exercises (I)

Apachecon Asia 2022 opens registration: pulsar technology issues make a big debut

Custom types: Enumeration

At & T pseudo instruction and interpretation of CFI CFA

2022年最新浙江建筑安全员模拟题库及答案

Template method mode

什么是模糊理论,基础,流程
随机推荐
Still shocked by the explosion in the movie? Then you must not miss this explosive plug-in of unity
SS-Paper【1】:Fully Convolutional Networks for Semantic Segmentation
工信安全中心牵头搭建数据流通平台 蚂蚁集团等厂商提供技术支持
Axi protocol (3): handshake mechanism and implementation details of Axi architecture
Internet Download Manager Configuration
[GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation
Envi SHP to ROI and mask the grid
Why can't sizeof (ARR) / size (arr[0]) be used to calculate the array length inside the called function?
jvm类加载子系统
MODIS 16 day data monthly / quarterly synthesis
Kernel development
Is it safe for Mr. qiniu to open a securities account? Can I drive it?
Exception handling - a small case that takes you to solve NullPointerException
Transformer structure analysis -- learning notes
js,for循环内callback异步转换成同步执行
会议OA项目进度(一)
Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation
AMD锐龙7000预计9月15日上市 3D缓存版还要再等等
DBF menu driver: librarydatabaseproject
Template method mode