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
@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
"Ambiguous Reference to Member Map" When Attempting to Append/Replace Array Element
How to Use @Fetchrequest Outside a View
Convert Time String into Date Swift
Cmlogitem Timestamp: Why So Complicated
Swift Nwlistener Listen, Cancel, and Relisten Successfully
Enum Initialized with a Non-Existent Rawvalue Does Not Fail and Return Nil
Enum of String Type Vs Struct with Static Constant
How to Create an iOS Liveview in Xcode 8/Swift 3
Cannot Convert [Int] to [Int] in Generic Implementation
How to Restore In-App Purchases Correctly
Finding The First Non-Repeating Character in a String Using Swift
Swiftui: What Are The Differences Between Image and UIimage
Alamofire Https Change in 10.3
Uipangesturerecognizer Is Not Working in iOS 13
All of My UIalertcontroller Messages Became Single Line