Created
July 23, 2024 09:52
-
-
Save demolaf/abd6af92bcfa9861d3cf76ffc509d2db to your computer and use it in GitHub Desktop.
Tab View with bottom line indicator (SwiftUI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
enum TabSection: CaseIterable { | |
case tab1 | |
case tab2 | |
var title: String { | |
switch self { | |
case .tab1: | |
"Tab 1" | |
case .tab2: | |
"Tab 2" | |
} | |
} | |
} | |
struct TabView: View { | |
@Binding var selectedTab: TabSection | |
@Namespace private var name | |
var body: some View { | |
HStack(spacing: 0) { | |
ForEach(TabSection.allCases, id: \.hashValue) { section in | |
Button { | |
selectedTab = section | |
} label: { | |
VStack { | |
Text(section.title.capitalized) | |
.font(.footnote) | |
.fontWeight(.medium) | |
.foregroundColor(selectedTab == section ? .appPrimary : Color(uiColor: .systemGray)) | |
ZStack { | |
Capsule() | |
.fill(.inactiveTab) | |
.frame(height: 2) | |
if selectedTab == section { | |
Capsule() | |
.fill(.appPrimary) | |
.frame(height: 2) | |
.matchedGeometryEffect(id: "Tab", in: name) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment