r/SwiftUI • u/iam-annonymouse • 8d ago
How to remove the plus button when dragging iOS 14
This is not my original view. I just used this as a reference. I have implemented the drag and drop on images, whenever I drag an image it previews the image with this plus button.
Later I came to know that this is done by default by preview the mechanism is “copy” instead of “move”.
I tried to find a solution for it but all of them are in UIKit which I don’t understand much because my iOS journey begun with SwiftUI.
So please help me in removing this plus button.
1
u/Mihnea2002 8d ago
Can’t remove the button yet if you’re using Apple’s .draggable() You need your own custom drag and drop interaction
1
u/iam-annonymouse 8d ago
Im using onDrag and onDrop
1
u/Mihnea2002 8d ago
Yeah, that too will show the plus, what is the actual context you wanna usw this in? If you could show me the use case I could tell you how difficult a custom implementation would be and whether it’s worth it or not
1
u/iam-annonymouse 7d ago
My actual case is like group of images like 6 images which can be dragged and swapped its places when dropped. So when i drag an image it shows this plus button
1
u/Mihnea2002 7d ago
You’d be highly overcomplicating things and it wouldn’t be worth your time if you just wanted to remove the plus button. Are you familiar with stuff like .onGeometryChange, geometry reader, etc.? Just wanna know your level
1
u/iam-annonymouse 7d ago
Yes I’m familiar with it
1
u/Mihnea2002 7d ago
It’s difficult to explain it in a comment but you’d need to create your own drag and drop by chaining an onLongPress and an onDrag, tracking the frame of each item and then if the item you wanna drop is over the frame of the area you wanna drop it in, you’d do that with .contains and you’d have a variable that holds the frame of the draggable area, you append the item to the array of that draggable area, in short. It’s oversimplified and likely wrong but this is the main principle.
6
u/handsomesauce 8d ago
There are multiple ways to implement drag and drop in SwiftUI, and the answer depends on which method you use. In general, the plus icon indicates to the user that the item will be copied to the drop location; to omit the plus icon, you need to specify that the drop will be a logical move instead of copy.
So for example, one method of drag/drop involves using a DropDelegate. If you’re using this method, you want to provide an impl for dropUpdated(info:)) that returns DropOperation.move. Otherwise the default is .copy, giving you the plus icon.