TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Picker won't change views

Forums > SwiftUI

I'm having a weird issue when I'm using a Picker and TabView to change views. I have the default selected menu choice to the second one (.two). When I run the code, if I select the first menu choice (.one), the view will not change, but if I select any other menu choice after, the view will change to the correct view. Does anyone have a solution to this?

Thanks for your help!!

enum MenuChoices: String, CaseIterable  {
    case one = "One"
    case two = "Two"
    case three = "Three"
}
struct HomeView: View {
  @State var currentMenu: MenuChoices = .two

  var body: some View {
    Picker("Menu", selection: $currentMenu) {
        ForEach(MenuChoices.allCases, id: \.self) { type in
            Text(type.rawValue.capitalized)
        }
    }
    .pickerStyle(.segmented)

    TabView(selection: $currentMenu, content: {
        Text("One").tag(MenuChoices.one)
        Text("Two").tag(MenuChoices.two)
        Text("Three").tag(MenuChoices.three)
    })
    .tabViewStyle(.page(indexDisplayMode: .never))
    .animation(.easeIn, value: currentMenu)
  }
}

2      

@COflatz I commented out the .tavViewStyle line and it seamsed to remove the problem.

Not a solution really as you loose the effect your after, but a place to start investigating perhaps.

I'm only new to SwiftUI so hopeuflly somneone else comes along soon with more info.

2      

@KitchenCrazy

I'm new to SwiftUI as well, but interesting that removing that fixed the issue. I will take it out for now, thank you!

I would like to keep the effect, if someone else has any other recommendation that keeps the effect that would be appreciated.

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.