Created
July 13, 2020 14:28
-
-
Save chriseidhof/b6dad3180e1d12be8919d1759bf7de3b to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// ContentView.swift | |
// TabBar | |
// | |
// Created by Chris Eidhof on 13.07.20. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
let tabs: [Text] = [ | |
Text("World Clock"), | |
Text("Alarm"), | |
Text("Bedtime") | |
] | |
@State var selectedTabIndex = 0 | |
@Namespace var ns | |
var body: some View { | |
HStack(alignment: .top) { | |
ForEach(tabs.indices) { tabIndex in | |
VStack { | |
Button(action: { | |
withAnimation(.default) { | |
self.selectedTabIndex = tabIndex | |
} | |
}, label: { self.tabs[tabIndex] }).padding(.bottom, 4) | |
.overlay(Group { | |
if selectedTabIndex == tabIndex { | |
VStack { | |
Spacer() | |
Rectangle() | |
.fill(Color.accentColor) | |
.matchedGeometryEffect(id: "indicator", in: ns) | |
.frame(height: 2, alignment: .bottom) | |
} | |
} | |
}) | |
} | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment