Picker Not Working When Editmode Is Active

Picker not working when editMode is active

As it looks such combination is impossible for now... Find below proposed variant of alternate solution - the idea is to use in-section-only button to activate reordering

demo2

    @State private var reorderMode: EditMode = .inactive
var body: some View {
Form {
Section(header: Text("General")) {
Picker("Sort by", selection: $sortBy) {
ForEach(SortBy.allCases, id: \.self) { sortBy in
Text(sortBy.name).tag(sortBy)
}
}
}

Section(header:
HStack {
Text("Phone order")
Spacer()
Button(self.reorderMode == .inactive ? "Reorder" : "Done") {
self.reorderMode = self.reorderMode == .active ? .inactive : .active
}
}
) {
ForEach(1..<10) { number in
Text("\(number)")
}
.onMove(perform: onMove)
.padding(.leading, self.reorderMode == .active ? -39 : 0) // remove space dedicated to delete button
}
}
.environment(\.editMode, $reorderMode)
.navigationBarTitle(Text("Sample View"))
}

SwiftUI: Conditional Context Menu Shown Unexpectedly

Works fine with Xcode 14 / iOS 16

Here is possible workaround for older versions (it is possible to try different places for .id modifier to have appropriate, acceptable, UI feedback)

Tested with Xcode 13.4 / iOS 15.5

Text("Long press me. Editing: \((editMode?.wrappedValue == .active).description)")
.contextMenu(editMode?.wrappedValue == .active ? nil : contextMenu)
.id(editMode?.wrappedValue) // << this !!

How to make styled picker fully touchable

Try to apply all size/frame related modifiers to internal label, instead of picker itself, like

   Picker(selection: $selectedProduct, label: 
Text("Product: \(self.products[self.selectedProduct].name!)")
.frame(minWidth: 0, maxWidth: .infinity)
.font(.headline)
.foregroundColor(Color.white)
.padding()
.background(Color(UIColor(.blue)))
.cornerRadius(15.0)
) {

ForEach(0 ..< roles.count) {
Text(products[$0].name!)
.frame(minWidth: 0, maxWidth: .infinity)
}

}
.animation(nil)
.pickerStyle(MenuPickerStyle())

SwiftUI List Selection: Have to Tap Twice to Deselect Item Initially

To prevent the double-click issue, set the selection in onAppear inside the List:

import SwiftUI

struct ContentView: View {
@State private var selectedItems: Set<Int> = []

var body: some View {
NavigationView { // Some apparent solutions do not work inside a NavigationView
List(selection: $selectedItems) {
Text("Item 1").tag(1)
Text("Item 2").tag(2)
Text("Item 3").tag(3).onAppear() { // onAppear inside the List!
selectedItems = [1]
}
}.environment(\.editMode, Binding.constant(EditMode.active))
}
}
}

Note that this sample code sets the same selection on every onAppear. Depending on how your app should behave you will have to change it.

If you have a ForEach in your List, you can attach the onAppear to it.



Related Topics



Leave a reply



Submit