Skip to content

Instantly share code, notes, and snippets.

@ryanlintott
Last active May 25, 2023 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlintott/7b7ae57b7739f306613052a1ea1941f2 to your computer and use it in GitHub Desktop.
Save ryanlintott/7b7ae57b7739f306613052a1ea1941f2 to your computer and use it in GitHub Desktop.
A list of items with accessibility action to move an item up.
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)
}
}
}
}
@ryanlintott
Copy link
Author

RPReplay_Final1684705151.mov

@ryanlintott
Copy link
Author

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