Skip to content

Instantly share code, notes, and snippets.

@joeblau
Created September 21, 2021 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeblau/0c6ed144c685f98a79ea1477697e5aee to your computer and use it in GitHub Desktop.
Save joeblau/0c6ed144c685f98a79ea1477697e5aee to your computer and use it in GitHub Desktop.
Segment Control + Pages
//
// ContentView.swift
// SegPage
//
// Created by Joe Blau on 9/21/21.
//
import SwiftUI
enum Page: Identifiable, CaseIterable, CustomStringConvertible {
public var id: Self { self }
case first, second, third
var description: String {
switch self {
case .first: return "First"
case .second: return "Second"
case .third: return "Third"
}
}
}
struct ContentView: View {
@State var pages: Page = .first
var body: some View {
VStack {
Picker("Current Page", selection: self.$pages) {
ForEach(Page.allCases) { page in
Text(page.description)
}
}
.pickerStyle(.segmented)
TabView(selection: self.$pages) {
FirstPageView()
.tag(Page.first)
SecondPageView()
.tag(Page.second)
ThirdPageView()
.tag(Page.third)
}
.tabViewStyle(PageTabViewStyle())
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
.animation(.easeInOut)
.transition(.slide)
}
}
}
struct FirstPageView: View {
var body: some View {
Text("First Page")
}
}
struct SecondPageView: View {
var body: some View {
Text("Second Page")
}
}
struct ThirdPageView: View {
var body: some View {
Text("Third Page")
}
}
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