SwiftUI's drop API is only fired under certain conditions?
Try dragging the "Drag Me" block over the others.
For some reason, a TextEditor needs to satisfy 2 conditions before it's "superview" can detect the drop?
- have a
background - disabled
struct ContentView: View {
@StateObject fileprivate var viewModel = ViewModel()
@State private var s1 = "Nope"
@State private var s2 = "Nope"
@State private var s3 = "Nope"
@State private var s4 = "I can detect"
@State private var s5 = "I can detect"
@State private var s6 = "Drag me"
var body: some View {
VStack(spacing: 10) {
TextEditor (text: $s1) // drop not detected
.disabled(true)
TextEditor (text: $s2) // drop not detected
.opacity(0.5)
.disabled(true)
TextEditor (text: $s3) // drop not detected
.opacity(0.5).background(Color.pink.opacity(0.5))
TextEditor (text: $s4) // drop detected
.background(Color.pink.opacity(0.5))
.disabled(true)
TextEditor (text: $s5) // drop detected
.opacity(0.5).background(Color.pink.opacity(0.5))
.disabled(true)
Text("Yes") // drop detected
.background(Color.pink.opacity(0.5))
TextEditor (text: $s6)
.onDrag { () -> NSItemProvider in
return NSItemProvider(object: "Hello" as NSString)
}
}.onDrop(of: [.plainText], delegate: viewModel)
}
}
fileprivate class ViewModel: NSObject, ObservableObject, DropDelegate {
func dropEntered(info: DropInfo) {
print("Drop entered: \(info)")
}
func dropUpdated(info: DropInfo) -> DropProposal? {
print("Drop updated: \(info)")
return DropProposal(operation: .move)
}
func dropExited(info: DropInfo) {
print("Drop exited: \(info)")
}
func performDrop(info: DropInfo) -> Bool {
print("Perform drop: \(info)")
return true
}
}
Tested on Xcode 12.3, iOS 14.3
Am I missing something or is this a bug?