回答編集履歴

1

追記

2021/09/07 08:31

投稿

shiokara
shiokara

スコア95

test CHANGED
@@ -25,3 +25,241 @@
25
25
  ```
26
26
 
27
27
  タブを生成しているこの場所の`3`の部分を`Defs.tabItems.count`に書き換えれば増やせると思います。
28
+
29
+
30
+
31
+ 追記
32
+
33
+ レイアウトを直した全体コード
34
+
35
+ ```Swift
36
+
37
+ import SwiftUI
38
+
39
+ import MapKit
40
+
41
+
42
+
43
+ struct TopFrameView: Shape {
44
+
45
+ func path(in rect: CGRect) -> Path {
46
+
47
+ let bezierPath = UIBezierPath()
48
+
49
+ bezierPath.move(to: CGPoint(x: 53.22, y: 4.36))
50
+
51
+ for (to, controlPoint1, controlPoint2) in [(CGPoint(x: 60.83, y: 13.06), CGPoint(x: 57.76, y: 7.77), CGPoint(x: 60.14, y: 11.68)), (CGPoint(x: 68.43, y: 22.84), CGPoint(x: 63, y: 17.4), CGPoint(x: 65.05, y:20.96)), (CGPoint(x: 75.16, y: 23.98), CGPoint(x: 70.49, y: 23.98), CGPoint(x: 75.16, y: 23.98))] {
52
+
53
+ bezierPath.addCurve(to: to, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
54
+
55
+ }
56
+
57
+ bezierPath.addLine(to: CGPoint(x: 0.84, y: 23.98))
58
+
59
+ for (to, controlPoint1, controlPoint2) in [(CGPoint(x: 7.57, y: 22.84), CGPoint(x: 0.84, y: 23.98), CGPoint(x: 5.51, y: 23.98)), (CGPoint(x: 15.17, y: 13.06), CGPoint(x: 10.95, y: 20.96), CGPoint(x: 13, y: 17.4)), (CGPoint(x: 22.78, y: 4.36), CGPoint(x: 15.86, y: 11.68), CGPoint(x: 18.24, y: 7.77)), (CGPoint(x: 36.38, y: -0), CGPoint(x: 27.58, y: 0.77), CGPoint(x: 33.55, y: 0.1)), (CGPoint(x: 38, y: 0), CGPoint(x: 37.39, y: -0.04), CGPoint(x: 38, y: 0)), (CGPoint(x: 53.22, y: 4.36), CGPoint(x: 38, y: 0), CGPoint(x: 46.7, y: -0.53))] {
60
+
61
+ bezierPath.addCurve(to: to, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
62
+
63
+ }
64
+
65
+ bezierPath.close()
66
+
67
+ return Path(bezierPath.cgPath)
68
+
69
+ }
70
+
71
+ }
72
+
73
+ struct TabItemDescription {
74
+
75
+ var imageName: String
76
+
77
+ var title: String
78
+
79
+ func iconView(_ foregroundColor: Color) -> some View { Image(systemName: imageName).font(.system(size: 24)).foregroundColor(foregroundColor) }
80
+
81
+ func labelView(_ foregroundColor: Color) -> some View { Text(title).font(.system(size: 9, weight: .bold)).foregroundColor(foregroundColor) }
82
+
83
+ }
84
+
85
+ enum Defs {
86
+
87
+ static let tabItems: [TabItemDescription] = [.init(imageName: "house.fill", title: "HOME"),
88
+
89
+ .init(imageName: "magnifyingglass", title: "SEARCH"),
90
+
91
+ .init(imageName: "person.fill", title: "PROFILE"),
92
+
93
+ .init(imageName: "magnifyingglass", title: "SEARCH"),
94
+
95
+ .init(imageName: "person.fill", title: "PROFILE")]
96
+
97
+ static let accentColor = Color(UIColor(red: 0, green: 0.251, blue: 0.6588, alpha: 1)); static let backgroundColor = Color(UIColor(red: 0.945, green: 0.969, blue: 0.984, alpha: 1.000))
98
+
99
+ static let topFrameSize = CGSize(width: 75, height: 24)
100
+
101
+ static let tabbarHeight = CGFloat(49)
102
+
103
+ static let bottomSafeArea = CGFloat(40)
104
+
105
+ static let iconCircleEdge = CGFloat(40)
106
+
107
+ static let labelOffset = CGSize(width: 0, height: 32)
108
+
109
+ static let bottomSafeAreaOffset = CGSize(width: 0, height: Defs.bottomSafeArea * 0.5)
110
+
111
+ }
112
+
113
+
114
+
115
+ struct ContentView: View {
116
+
117
+ @State var selectedIndex = 0
118
+
119
+ var body: some View {
120
+
121
+ ZStack {
122
+
123
+ Text("Welcome").font(.largeTitle) // main contents example
124
+
125
+ GeometryReader { proxy in
126
+
127
+ VStack {
128
+
129
+ Spacer()
130
+
131
+ HStack(alignment: .bottom, spacing: 0){
132
+
133
+ ForEach(0..<Defs.tabItems.count) { (index) in
134
+
135
+ VStack(spacing: 0) {
136
+
137
+ Spacer()
138
+
139
+ Rectangle()
140
+
141
+ .foregroundColor( .clear )
142
+
143
+ .frame(width: Defs.iconCircleEdge, height: Defs.iconCircleEdge)
144
+
145
+ .overlay(
146
+
147
+ ZStack {
148
+
149
+ Defs.tabItems[index].iconView(.white).opacity(self.selectedIndex == index ? 1.0 : 0.0)
150
+
151
+ Defs.tabItems[index].iconView(Defs.accentColor).opacity(self.selectedIndex != index ? 1.0 : 0.0)
152
+
153
+ }
154
+
155
+ )
156
+
157
+ .background(
158
+
159
+ ZStack {
160
+
161
+ Defs.tabItems[index].labelView(.black).opacity(self.selectedIndex == index ? 1.0 : 0.0)
162
+
163
+ Defs.tabItems[index].labelView(.clear).opacity(self.selectedIndex != index ? 1.0 : 0.0)
164
+
165
+ }.offset(Defs.labelOffset)
166
+
167
+ )
168
+
169
+ Rectangle()
170
+
171
+ .foregroundColor(.clear)
172
+
173
+ .frame(height: self.selectedIndex == index ? 26 : 5)
174
+
175
+ }
176
+
177
+ .frame(width: proxy.size.width * 0.2)
178
+
179
+ .contentShape( Rectangle() )
180
+
181
+ .onTapGesture {
182
+
183
+ withAnimation(.interactiveSpring(response: 0.4, dampingFraction: 0.7) ) { self.selectedIndex = index }
184
+
185
+ }
186
+
187
+ }
188
+
189
+ }
190
+
191
+ .background(
192
+
193
+ Rectangle()
194
+
195
+ .frame(height: Defs.topFrameSize.height + Defs.tabbarHeight + Defs.bottomSafeArea)
196
+
197
+ .overlay(
198
+
199
+ Circle()
200
+
201
+ .foregroundColor(Defs.accentColor)
202
+
203
+ .frame(width: Defs.iconCircleEdge, height: Defs.iconCircleEdge)
204
+
205
+ .offset(CGSize(width: CGFloat(self.selectedIndex - 2) * (proxy.size.width * 0.2), height: -29))
206
+
207
+ )
208
+
209
+ .foregroundColor(.white)
210
+
211
+ .offset(Defs.bottomSafeAreaOffset)
212
+
213
+ .mask(
214
+
215
+ VStack(spacing: 0) {
216
+
217
+ TopFrameView()
218
+
219
+ .frame(width: Defs.topFrameSize.width, height: Defs.topFrameSize.height)
220
+
221
+ .offset(CGSize(width: CGFloat(self.selectedIndex - 2) * (proxy.size.width * 0.2), height: 0))
222
+
223
+ Rectangle()
224
+
225
+ .frame(height: Defs.tabbarHeight + Defs.bottomSafeArea)
226
+
227
+ }.offset(Defs.bottomSafeAreaOffset)
228
+
229
+ )
230
+
231
+ .shadow(color: Color.black.opacity(0.3) , radius: 15, x: 0, y: 0)
232
+
233
+ )
234
+
235
+ .frame(height: Defs.topFrameSize.height + Defs.tabbarHeight)
236
+
237
+ }
238
+
239
+ }
240
+
241
+ .ignoresSafeArea(edges: [.trailing, .leading])
242
+
243
+ }
244
+
245
+ .background(Defs.backgroundColor)
246
+
247
+ }
248
+
249
+ }
250
+
251
+
252
+
253
+ struct TopFrameView_Previews: PreviewProvider {
254
+
255
+ static var previews: some View {
256
+
257
+ ContentView()
258
+
259
+ }
260
+
261
+ }
262
+
263
+
264
+
265
+ ```