It appears that SwiftUI now prevents Button
s from being dragged. This used to work in Xcode 15.4 and Xcode 16 beta 1-2.
Basically, if the draggable
view modifier is associated with a Button
, it can no longer be dragged. I have several uses cases where sometimes I want to have an entity behave as a button, but also be draggable.
Example shows case where a Button is draggable
. There’s also a simple VStack
that is draggable. The VStack
works, the button does not.
import Foundation
import CoreTransferable
import UniformTypeIdentifiers
extension UTType {
static let entityReference: UTType = UTType(exportedAs: "com.mycompany.MyApp.entity-reference")
}
struct EntityReference: Codable, Transferable {
let identifier: String
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .entityReference)
}
}
struct ContentView: View {
var body: some View {
VStack {
Button {
print("Button pressed")
} label: {
VStack {
Text("I'm a button. Try to drag me.")
.font(.title)
}
.border(.black)
.padding()
}
.draggable(EntityReference(identifier: "some-id"))
VStack {
Text("NOT a button. Try to drag me too.")
.font(.title)
}
.border(.black)
.padding()
.draggable(EntityReference(identifier: "some-id"))
VStack {
Text("Drop area")
.font(.title)
}
.border(.black)
.padding()
.dropDestination(for: EntityReference.self) { droppedEntityReferences, location in
print("Dropped something here!")
return true
}
}
}
}
#Preview {
ContentView()
}
I presume that I can maybe work around this by not using Buttons, but this is not really great for accessibility without adding more custom traits.
Submitted bug report: FB14518001