当前位置:网站首页>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 !
边栏推荐
- 【GNN报告】腾讯AI lab 徐挺洋:图生成模型及其在分子生成中的应用
- 量化框架backtrader之一文读懂Indicator指标
- What should we pay attention to in the resume of software testing?
- 1024 happy holidays
- IP第十三天笔记
- Pat class A - check in and check out
- Comparison of array and object merging methods assign, merge, defaults, defaultsdeep in lodash
- What exactly is API?
- js,for循环内callback异步转换成同步执行
- ArcGIS pixel size changed from 0.00025 to meters
猜你喜欢

ArcGIS pixel size changed from 0.00025 to meters

剑指 Offer 48. 最长不含重复字符的子字符串
[redis] -1. two ways of setting up environment based on docker

Custom types: Enumeration

portfwd 端口转发

Long awaited full platform support - Open Source im project uniapp update of openim

MODIS data WGet Download

Implementation of side list menu (side menu) of wechat applet
Shardingsphere database read / write separation
![[matlab]: basic knowledge learning](/img/8c/d13597e402c55df6cbd5e008aef0a5.png)
[matlab]: basic knowledge learning
随机推荐
工信安全中心牵头搭建数据流通平台 蚂蚁集团等厂商提供技术支持
Problems encountered in upgrading chrome to version 80 - solutions to system login failure
OS Experiment 5 process switching based on kernel stack switching
代码随想录笔记_链表_707设计链表
自定义类型:枚举
C font usage effect
Parental delegation mechanism
I'll teach you how to use NPs to build intranet penetration services. When you go out, you can easily connect your lightweight notebook to your home game console to play remotely
Zcmu--5083: number pairs of ly (C language)
1309. Decode letter to integer mapping
The third edition of New Horizon College English reading and Writing Tutorial 4 graduation examination site (units 1,2,3,5,6)
Jing Wei PS tutorial: basic part a
双亲委派机制
[matlab]: basic knowledge learning
Concept of IP, classification of IP, IP multiplexing technology
Live review | wonderful playback of Apache pulsar meetup (including PPT download)
Buffer overflow vulnerability lab experiment record
Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation
regular expression
NATBypass 端口转发