@@ -23,6 +23,7 @@ struct HorizontalScrollView: View {
23
23
@Environment ( \. horizontalSizeClass) var horizontalSizeClass
24
24
@Environment ( \. verticalSizeClass) var verticalSizeClass
25
25
@State private var offset : CGFloat = 0
26
+ @State private var geometryData : CGRect = . zero
26
27
27
28
private var darkGreen = Color ( red: 0 / 255 , green: 102 / 255 , blue: 0 / 255 )
28
29
private var darkRed = Color ( red: 220 / 255 , green: 20 / 255 , blue: 60 / 255 )
@@ -31,12 +32,20 @@ struct HorizontalScrollView: View {
31
32
var body : some View {
32
33
ScrollView {
33
34
VStack {
34
- HStack {
35
- Text ( " Your Width " )
36
- Text ( UIScreen . main. bounds. size. width. description)
37
- Text ( " Height " )
38
- Text ( UIScreen . main. bounds. size. height. description)
35
+ Text ( " Geometry Width " )
36
+ GeometryReader { geometry in
37
+ VStack {
38
+ Text ( " \( Int ( geometry. size. width) ) " )
39
+ }
40
+ . onAppear {
41
+ self . geometryData = geometry. frame ( in: . global)
42
+ }
43
+ . frame ( maxWidth: . infinity, alignment: . center)
39
44
}
45
+ Text ( " \n Screen Width " )
46
+ Text ( UIScreen . main. bounds. size. width. description)
47
+ Text ( " Screen Height " )
48
+ Text ( UIScreen . main. bounds. size. height. description)
40
49
Text ( " WCAG's Reflow success criterion requires no horizontal scrolling for vertically scrolling content at a width of 320 pixels or less. When horizontal scrolling is used then it must have single tap alternatives to the gestures used to scroll the content. " )
41
50
. padding ( . bottom)
42
51
Text ( " Good Example " )
@@ -49,8 +58,8 @@ struct HorizontalScrollView: View {
49
58
. frame ( height: 2.0 , alignment: . leading)
50
59
. background ( colorScheme == . dark ? Color ( . systemGreen) : darkGreen)
51
60
. padding ( . bottom)
52
- let layout = ( UIScreen . main. bounds. size. width <= 320 ) ? AnyLayout ( VStackLayout ( ) ) : AnyLayout ( HStackLayout ( ) )
53
- if UIScreen . main. bounds. size. width <= 320 {
61
+ let layout = ( UIScreen . main. bounds. size. width <= 320 || geometryData . size . width <= 288 ) ? AnyLayout ( VStackLayout ( ) ) : AnyLayout ( HStackLayout ( ) )
62
+ if UIScreen . main. bounds. size. width <= 320 || geometryData . size . width <= 288 {
54
63
} else {
55
64
HStack {
56
65
Spacer ( )
@@ -69,13 +78,13 @@ struct HorizontalScrollView: View {
69
78
}
70
79
. frame ( maxWidth: . infinity, alignment: . trailing)
71
80
}
72
- ScrollView ( UIScreen . main. bounds. size. width <= 320 ? . vertical : . horizontal) {
81
+ ScrollView ( UIScreen . main. bounds. size. width <= 320 || geometryData . size . width <= 288 ? . vertical : . horizontal) {
73
82
layout {
74
83
ForEach ( 0 ..< 10 ) { i in
75
84
Button ( action: {
76
85
// Handle button action
77
86
} ) {
78
- Text ( UIScreen . main. bounds. size. width <= 320 ? " Vertical Button \( i) " : " Horizontal Button \( i) " )
87
+ Text ( UIScreen . main. bounds. size. width <= 320 || geometryData . size . width <= 288 ? " Vertical Button \( i) " : " Horizontal Button \( i) " )
79
88
. padding ( )
80
89
}
81
90
. background (
@@ -88,7 +97,7 @@ struct HorizontalScrollView: View {
88
97
}
89
98
}
90
99
. scrollTargetLayout ( )
91
- . offset ( x: UIScreen . main. bounds. size. width <= 320 ? 0 : offset)
100
+ . offset ( x: UIScreen . main. bounds. size. width <= 320 || geometryData . size . width <= 288 ? 0 : offset)
92
101
}
93
102
. scrollTargetBehavior ( . viewAligned)
94
103
. safeAreaPadding ( . horizontal, 40 )
0 commit comments