Skip to content

Commit

Permalink
👍 issue #3 ボトムバーアイコンで、選択中のものをのみ灰色から緑色に変化するように実装
Browse files Browse the repository at this point in the history
  • Loading branch information
tora-muscle committed Feb 9, 2023
1 parent 6d3d84c commit 9d34b2f
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions 1weekPDCA/presentation/component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,32 @@ import Foundation
import SwiftUI


// TabアイコンとViewを設定
// 現時点ではText()でモック化
let tabItems: [TabItem] = [
TabItem(image: "graduationcap.fill", view: AnyView(Text("ActTabView()"))),
TabItem(image: "square.fill", view: AnyView(Text("PlanDoTabView()"))),
TabItem(image: "checkmark.circle.fill", view: AnyView(Text("CheckTabView()")))
]

// ボトムバーの実装
struct BottomBar: View {

// ボトムバーの状態管理
@State private var selectedIndex = 0

// ボトムバーのアイコン、ページ(view)
let tabItems: [TabItem] = [
TabItem(image: "graduationcap.fill", view: AnyView(Text("ActTabView()"))),
TabItem(image: "square.fill", view: AnyView(Text("PlanDoTabView()"))),
TabItem(image: "checkmark.circle.fill", view: AnyView(Text("CheckTabView()")))
]

// ボトムバーの実装
var body: some View {
TabView {
// `image`をid(識別子)として反復処理を実装
ForEach(tabItems, id: \.image) { item in
item.view
TabView(selection: $selectedIndex) {
// tabItems.count だと定数じゃないから数値を指定しろって怒られた
ForEach(0 ..< 3) { index in
// その index の view を表示
self.tabItems[index].view
.tabItem {
Image(systemName: item.image)
Image(systemName: self.tabItems[index].image)
.renderingMode(.template)
.foregroundColor(Color("UIColorGray"))
.foregroundColor(self.selectedIndex == index ? Color("UIColorGreen") : Color("UIColorGray"))
}
// 各 view にタグをつけて選択状態を管理
.tag(index)
}
}
}
Expand Down

0 comments on commit 9d34b2f

Please sign in to comment.