A list of items with accessibility action to move an item up.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ListWithMoveUpAction: View { | |
@AccessibilityFocusState private var focus: String? | |
@State private var users = ["Glenn", "Malcolm", "Nicola", "Terri"] | |
var body: some View { | |
NavigationStack { | |
List(users, id: \.self) { user in | |
Text(user) | |
.accessibilityActions { | |
Button { | |
if let firstIndex = users.firstIndex(of: user), | |
firstIndex > 0 { | |
/// Save a copy of the user here | |
let thisUser = user | |
users.swapAt(firstIndex, users.index(before: firstIndex)) | |
/// Set the focus to the copy (setting the focus to user does not work) | |
focus = thisUser | |
} | |
} label: { | |
Text("Move Up") | |
} | |
} | |
.accessibilityFocused($focus, equals: user) | |
} | |
} | |
} | |
} |
You can find a full example of list items with drag and drop, move, etc with an updated version of this accessibility action here: https://github.com/ryanlintott/DragAndDrop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RPReplay_Final1684705151.mov