Overview 概要
Use this capability to request that VoiceOver or other accessibility technologies programmatically focus on a specific element, or to determine whether VoiceOver or other accessibility technologies are focused on particular elements. Use accessibility
or accessibility
in conjunction with this property wrapper to identify accessibility elements for which you want to get or set accessibility focus. When accessibility focus enters the modified accessibility element, the framework updates the wrapped value of this property to match a given prototype value. When accessibility focus leaves, SwiftUI resets the wrapped value of an optional property to nil
or the wrapped value of a Boolean property to false
. Setting the property’s value programmatically has the reverse effect, causing accessibility focus to move to whichever accessibility element is associated with the updated value.
In the example below, when notification
changes, and its is
property is true
, the accessibility focus moves to the notification Text
element above the rest of the view’s content:
struct CustomNotification: Equatable {
var text: String
var isPriority: Bool
}
struct ContentView: View {
var notification: CustomNotification?
var isNotificationFocused: Bool
var body: some View {
VStack {
if let notification = self.notification {
Text(notification.text)
.accessibilityFocused($isNotificationFocused)
}
Text("The main content for this view.")
}
.onChange(of: notification) { notification in
if (notification?.isPriority == true) {
isNotificationFocused = true
}
}
}
}
To allow for cases where accessibility focus is completely absent from the tree of accessibility elements, or accessibility technologies are not active, the wrapped value must be either optional or Boolean.
Some initializers of Accessibility
also allow specifying accessibility technologies, determining to which types of accessibility focus this binding applies. If you specify no accessibility technologies, SwiftUI uses an aggregate of any and all active accessibility technologies.