Created
November 29, 2022 18:14
-
-
Save allfinlir/371acff409c2b60827e3dfb21934ff9a 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
import SwiftUI | |
struct SelectedButton: View { | |
@State var tabButtons = tabs[0] | |
var body: some View { | |
VStack { | |
ScrollView(.horizontal, showsIndicators: false) { | |
ScrollViewReader { value in | |
HStack(spacing: 20) { | |
ForEach(tabs, id: \.self) { tab in | |
Button(action: { | |
tabButtons = tab | |
value.scrollTo(tab) | |
}, label: { | |
Text(tab) | |
.font(.title2) | |
.foregroundColor(tabButtons == tab ? .black : .secondary) | |
.background( | |
ZStack { | |
if tabButtons == tab { | |
Rectangle() | |
.frame(height: 5) | |
.offset(y: 25) | |
.foregroundColor(.green) | |
} | |
} | |
) | |
}) | |
.animation(.linear(duration: 0.2), value: tabButtons) | |
} | |
} | |
.padding() | |
} | |
} | |
ZStack { | |
Color.gray | |
.opacity(0.1) | |
VStack { | |
Text("Hello") | |
.font(.largeTitle) | |
} | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
} | |
.padding(.top, 5) | |
} | |
} | |
} | |
struct SelectedButton_Previews: PreviewProvider { | |
static var previews: some View { | |
SelectedButton() | |
} | |
} | |
var tabs: [String] = ["Overview", "Orders", "Transactions", "News", "Contacts", "My Account"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment