PickerExample not working when using .segmented style
JuliusHuizing opened this issue · comments
Very nice package Andrea.
I did notice however that when I try to use a segmented picker within a partial sheet, the picker no longer seems to work.
e.g here I change your picker example to use a segmented style instead of the wheel style, and you should see the picker no longer seems to bind/change the selected item:
`//
// PickerExample.swift
// PartialSheetExample
//
// Created by Rasmus Styrk on 14/08/2020.
// Copyright © 2020 Swift. All rights reserved.
//
import SwiftUI
import PartialSheet
struct PickerExample: View {
@State private var isSheetPresented = false
var body: some View {
HStack {
Spacer()
PSButton(
isPresenting: $isSheetPresented,
label: {
Text("Display the PickerExample Sheet")
})
.padding()
Spacer()
}
.partialSheet(isPresented: $isSheetPresented,
content: PickerSheetView.init)
.navigationBarTitle("Picker Example")
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct PickerExample_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
PickerExample()
}
.attachPartialSheetToRoot()
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct PickerSheetView: View {
var strengths = ["Low", "Medium", "High"]
@State private var selectedStrength = 0
var body: some View {
VStack {
VStack {
Text("Settings Panel").font(.headline)
Spacer()
Text("Wheel Picker").font(.callout)
Picker(selection: $selectedStrength, label: EmptyView()) {
ForEach(0 ..< strengths.count) {
Text(self.strengths[$0])
.font(.callout)
}
}.pickerStyle(.segmented)
// .pickerStyle(.wheel)
.frame(height: 100)
.clipped(antialiased: true)
.padding(1)
.background(
RoundedRectangle(cornerRadius: 5)
.fill(.clear)
.border(Color.gray)
)
Spacer()
Text("Inline Picker").font(.callout)
Picker(selection: $selectedStrength, label: EmptyView()) {
ForEach(0 ..< strengths.count) {
Text(self.strengths[$0])
.font(.callout)
}
}.pickerStyle(.segmented)
Spacer()
}
.padding()
.frame(height: 350)
}
}
}
`
I've noticed this as well. Segmented Pickers and Steppers don't seem to work in the partial sheet.
My leading theory is that the drag gesture recognizer (from Partial Sheet) is conflicting with the tap gestures for the picker/stepper buttons (from SwiftUI). I think this because I also noticed just having a normal tap gesture of a view that contains a Stepper or Picker causes issue. Example:
struct ContentView: View {
@State var toggle = false
@State var count: Int = 1
@State var picker = "A"
var body: some View {
VStack {
Toggle("Toggle", isOn: $toggle)
Stepper("Stepper: \(count)", value: $count, in: 1...5)
Picker("Picker", selection: $picker) {
Text("A").tag("A")
Text("B").tag("B")
Text("C").tag("C")
}
.pickerStyle(.segmented)
Spacer()
}
.onTapGesture {
print("Tap recognized")
}
}
}
Ultimately, this seems like a SwiftUI bug. 🤔
Same issue here.
@ajevans99 your instinct seems to have been correct; following your lead I managed to get it working by simply deleting a call to a hard-coded onTapGesture method (that fortunately does not seem to execute anything) in the iPhoneSheet builder. This solves the issue for me.
@AndreaMiotto I opened a PR to solve this issue: #165
Thanks to your very cleanly organised code and docs, the culprit was very easy to find @AndreaMiotto - so cheers for that!