UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Picker - SegmentedPickerStyle only accepts long presses

Forums > SwiftUI

Straight forward view :

import SwiftUI
import UIKit
struct Reports: View {
    @State private var favoriteColor = "Red"

    var body: some View {
        VStack {
            Picker(selection: $favoriteColor, label: Text("What is your favorite color?")) {
                Text("Red").tag("Red")
                Text("Green").tag("Green")
                Text("Blue").tag("Blue")
            }
            .pickerStyle(SegmentedPickerStyle())
            .onTapGesture {
                print("tap \(self.favoriteColor)")
            }

            Text("Value: \(favoriteColor)").navigationBarTitle("Picker")
        }
    }

}

But in simulator and on phone, tapping prints in console but doesn't change selector. However, if I longpress on the segments it works.

This is a new view as part of a larger project. If I create a new project and paste this in, then it behaves fine.

So I'm assuming there may be something elsewhere controlling this? Paul

3      

So after cutting and pasting an appdelegate and scenedelegate from a working project, i played spot the difference.

My default contentView is pointing to MainMenu() which is a view that has tabviews that load various new views whatever tab is selected. If I revert back to ContentView.swift, it works as ContentView contains my Picker. But that is nolonger my landing page. My start up page is MainMenu. But if I use that, the Picker doesn't work the way it should.

I'm new to this. Is this the best way to do this? Have one view that has my TabView's set up (not inside a NavigationView), then new views that use NavigationView.

3      

Hey,

Could you perhaps host your project (or at least enough for us to be able to reproduce the issue) somewhere online, preferably on GitHub, so we can look through it and try to find an issue? Thanks

--Jakub

3      

Hey Jakub,

Thanks for this - i've pushed it now to GitHub - https://github.com/pjnorris/Tasker/tree/master/Tasker

Summary:

ContentView() - is now my default start page, this contains TabViews, which branch out to four other file views

EntryView() - this contains the Picker - only accepts LONG presses on the Segmented control

Reports() - this is a Test view for Picker so that I can troubleshoot, exhibits the same issue as Entry()

If I change the default view in SceneDelegate to Reports() or EntryView() the segmented Picker behaves normally. It's only when its loaded from the TabView in ContentView that the Picker behaves the way it does.

Thanks in advance, Paul

3      

Hello Paul,

the problem seems to be linked to the use of .onTapGesture modifier which have strange effectes on Picker.

Here is some code to produce the wanted effect :

    var body: some View {
        let pref = Binding<String>(
            get: {
                favoriteColor
            },
            set: {
                favoriteColor = $0
                print("favoriteColor = \"\(favoriteColor)\"")
            }
        )

        VStack {
            Picker(selection: pref, label: Text("What is your favorite color?")) {
                Text("Red").tag("Red")
                Text("Green").tag("Green")
                Text("Blue").tag("Blue")
            }
            .pickerStyle(SegmentedPickerStyle())

            Text("Value: \(favoriteColor)").navigationBarTitle("Picker")

        }

    }

This is inspired by this tutorial by another Paul Hope it helps. Denis

3      

Hi,

Thanks for the clue. It was the .onTapGesture that was causing the issue.

Although I couldn't get your code to work, I got the general gist of it and was able to return the Picker with the Binding String. But the effect was still present.

Solution was to comment out the onTapGesture - turns out I didn't need them!

 TabView(selection: $selection) {
            EntryView().environmentObject(items)
             //   .onTapGesture { self.selection = 1 }
                .tabItem {
                        Image(systemName: "text.badge.plus")
                        Text("Entry")
                }.tag(1)
            TaskEntries().environmentObject(items)
          //      .onTapGesture { self.selection = 2 }
                .tabItem {
                        Image(systemName: "doc.plaintext")
                        Text("Entires")
                }.tag(2)
            Reports().environmentObject(items)
           //     .onTapGesture { self.selection = 3 }
                .tabItem {
                        Image(systemName: "doc.plaintext")
                        Text("Reports")
                }.tag(3)
            Settings().environmentObject(items)
         //       .onTapGesture { self.selection = 4 }
                .tabItem {
                        Image(systemName: "gear")
                        Text("Settings")
                }.tag(4)

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.