Skip to content

Commit

Permalink
refac: 탭 메뉴를 열거형으로 변경 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinsujin committed Feb 28, 2023
1 parent f23f9da commit d5478ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 14 additions & 2 deletions TabMoving/TabMoving/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ struct ListView: View {
}
}

enum Menu: Int, CaseIterable {
case menu1 = 0
case menu2 = 1

var title: String {
switch self {
case .menu1: return "메뉴1"
case .menu2: return "메뉴2"
}
}
}

struct ContentView: View {
@State private var selectedIndex = 0
private let menus = ["메뉴1", "메뉴2"]
@State private var selectedIndex = Menu.menu1.rawValue
private let menus: [Menu] = Menu.allCases

var body: some View {

Expand Down
8 changes: 4 additions & 4 deletions TabMoving/TabMoving/HeaderTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

struct HeaderTabView: View {

let menus: [String]
let menus: [Menu]

@Binding var activeIndex: Int
@State private var barX: CGFloat = 0
Expand All @@ -18,7 +18,7 @@ struct HeaderTabView: View {
// -> widths[activeIndex] 로 접근 가능
init(
activeIndex: Binding<Int>,
menus: [String],
menus: [Menu],
fullWidth: CGFloat,
spacing: CGFloat,
horizontalInset: CGFloat
Expand All @@ -45,7 +45,7 @@ struct HeaderTabView: View {
barX = buttonLeadings[row]
}
} label: {
Text(menus[row])
Text(menus[row].title)
.frame(maxWidth: buttonWidth)
// .border(.orange, width: 1)
.foregroundColor(.black)
Expand Down Expand Up @@ -76,7 +76,7 @@ struct HeaderTabView_Previews: PreviewProvider {
static var previews: some View {
HeaderTabView(
activeIndex: .constant(0),
menus: ["menu-A", "munu-B"],
menus: [.menu1, .menu2],
fullWidth: UIScreen.main.bounds.width,
spacing: 40,
horizontalInset: 10)
Expand Down

0 comments on commit d5478ef

Please sign in to comment.