UIActivityIndicatorView
subclass in Swift
The implementation of the accessibilityLabel
getter in the UIActivityIndicatorView
is dynamic based on the state of the control. Therefore, if you set its accessibilityLabel
, it may change later.
The following subclass of UIActivityIndicatorView
overrides the standard accessibilityLabel
implementation. It is based on @ChrisCM's answer in Objective C.
class MyActivityIndicatorView: UIActivityIndicatorView { override var accessibilityLabel: String? { get { if isAnimating { return NSLocalizedString("ACTIVITY_INDICATOR_ACTIVE", comment: ""); } else { return NSLocalizedString("ACTIVITY_INDICATOR_INACTIVE", comment: ""); } } set { super.accessibilityLabel = newValue } } }
In my application, the activity indicator is displayed on the screen, and VoiceOver - only during animation. Therefore, I need only one accessibilityLabel
value. The following subclass uses the default dynamic implementation of the default accessibilityLabel
, unless explicitly specified. If set, it uses this value regardless of state.
class MyActivityIndicatorView: UIActivityIndicatorView { private var accessibilityLabelOverride: String? override var accessibilityLabel: String? { get { if accessibilityLabelOverride != nil { return accessibilityLabelOverride } return super.accessibilityLabel } set { accessibilityLabelOverride = newValue } } }
source share