Given the following code:
struct CopyButtonStyle: ButtonStyle {
init() {}
func makeBody(configuration: Configuration) -> some View {
let copyIconSize: CGFloat = 24
return Image(systemName: "doc.on.doc")
.renderingMode(.template)
.resizable()
.frame(width: copyIconSize, height: copyIconSize)
.accessibilityIdentifier("copy_button")
.opacity(configuration.isPressed ? 0.5 : 1)
}
}
I’m getting the following error:
‘accessibilityIdentifier’ is only available in iOS 14.0 or newer use
on iOS 14
When looking at the accessibilityIdentifier
declaration, I found this:
public func accessibilityIdentifier(_ identifier: String) -> ModifiedContent<Self, AccessibilityAttachmentModifier>
Xcode suggest enclosing either the whole button style struct or at least the makeBody
function to make it available on iOS 14+.
Is there a way to create an additional function, e.g.:
func addAccessibilityIdentifierIfAvailable(entryParam ??) -> some View
or similar that will return just the same view if the accessibilityIdentifier
is not available or return a view with the set identifier if it’s possible to set on that OS version.
2
Answers
One of the solutions:
Call site:
Here is a simple way for you: