Created
April 4, 2023 08:22
-
-
Save fl034/970869befe8f8a3a4947ea97a6fb046d to your computer and use it in GitHub Desktop.
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 Tab: String, Identifiable, Hashable, CaseIterable { | |
case green, yellow, red | |
var id: String { rawValue } | |
} | |
struct ContentView: View { | |
// Starting with last element. Changing this to .green doesn't work, | |
// except when swiped to or yellow was called once. | |
@State var selection: Tab = .red | |
var body: some View { | |
NavigationStack { | |
TabView(selection: $selection) { | |
Color.green.opacity(0.5) | |
.tag(Tab.green) | |
Color.yellow.opacity(0.5) | |
.tag(Tab.yellow) | |
Color.red.opacity(0.5) | |
.tag(Tab.red) | |
} | |
.tabViewStyle(.page(indexDisplayMode: .never)) | |
.navigationTitle("Test") | |
.toolbar { | |
ToolbarItem { | |
Picker(selection: $selection) { | |
ForEach(Tab.allCases) { element in | |
Text(element.rawValue) | |
.tag(element) | |
} | |
} label: { | |
Text(selection.rawValue) | |
} | |
.pickerStyle(.menu) | |
} | |
} | |
} | |
} | |
} | |
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