Disable Movement For Specific Items In An Editable List
Daily Coding Tip 238
It’s possible to prevent items in a list from being rearranged after an EditButton has been tapped.
While the .onMove modifier allows you to specify how items should be rearranged, .moveDisabled lets you specify whether it should be prevented. I’m starting with the most basic Item type conforming to Identifiable, because it has a unique id property. I want my items to conform to this protocol so I can use them in a ForEach without needing to specify an id explicitly.
It’s necessary to have this in a NavigationView or a NavigationStack if iOS is your minimum supported OS version. Otherwise you won’t have anywhere for the EditButton to appear, and this is necessary to allow the items in the List to be rearranged.
If you enable a Toggle to lock an item after the EditButton has been tapped, there will be an icon showing all rows can be dragged even though the locked item cannot be dragged.
If you tap the EditButton when an item was already locked, the icon to show it can be dragged is not shown:
This may influence your decision in terms of when to allow the user to control this ability.




