Date:

Share:

Adjust SwiftUI controls for the Button Shapes accessibility setting

Related Articles

Some iPhone users prefer to see clear edges and borders of buttons, so they stand out more among the usual labels in the user interface. They can turn on the “Button Shapes” toggle in their device’s accessibility settings, so the system can change the appearance of the controls to suit the user’s needs.


Screenshot of the iPhone's display and text accessibility settings with the Button Shapes button enabled

In many cases SwiftUI automatically adjusts the appearance of our controls to match the user’s settings, for example, when we use Button Views with built-in styles or a frame are provided NavigationLink and Link Controls.

A button in SwiftUI with an auto-style will resolve to a button with a border on devices where the “Button Shapes” setting is enabled.

Button("Open settings") {
    
}
.buttonStyle(.automatic)
Button with borderless style fixed when button shapes setting is disabled and border style when setting is enabled
Button with borderless style fixed when button shapes setting is disabled and border style when setting is enabled

But sometimes we need to adjust the appearance of our controls manually to respond to the accessibility setting. This can be necessary when we define custom button styles or embed links inside Text Views. To know if the “Button Shapes” setting is enabled, we can read the accessibilityShowButtonShapes environmental value.

struct ContentView: View {
    @Environment(.accessibilityShowButtonShapes)
    private var accessibilityShowButtonShapes

    var body: some View {
        ...
    }
}
  • Discover different ways to add SwiftUI views to existing UIKit projects
  • Use Xcode previews when designing and building a user interface
  • Update your UIKit apps with iOS 16 features such as Swift Charts and lock screen widgets
  • Move larger parts of your apps to SwiftUI while reusing UIKit’s built-in views and controllers

Learn more…

#

Customize custom button styles

When creating custom button styles that do not give a prominent shape to the button, we should consider adding a background, border, or underline to the button when accessibilityShowButtonShapes set to true.

struct ScalingButtonStyle: ButtonStyle {
    @Environment(.accessibilityShowButtonShapes)
    private var accessibilityShowButtonShapes
    
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .foregroundColor(.blue)
            .padding()
            .border(
                .blue,
                width: accessibilityShowButtonShapes ? 1 : 0
            )
            .scaleEffect(configuration.isPressed ? 1.5 : 1)
            .animation(.default, value: configuration.isPressed)
    }
}

Independent Link Views and NavigationLinkThey are automatically adjusted to respond to the “Button Shapes” setting. But links are embedded inside Text Views using Markdown or AttributedString Do not change their appearance.

VStack(spacing: 60) {
    Link(
        "Visit our website",
        destination: URL(
            string: "https://example.com"
        )!
    )
    
    Text("""
    [Visit our website](https://example.com) 
    to learn more about our products.
    """)
}
.multilineTextAlignment(.center)
A link display with a bordered button style when the Button Shapes setting is turned on and a text display with a link inside that is simply colored blue
A link display with a bordered button style when the Button Shapes setting is turned on and a text display with a link inside that is simply colored blue

We can make the link within the text more prominent by adding an underscore when accessibilityShowButtonShapes defined as true. We can do this by wrapping it separately Text display and make use of LocalizedStringKey.StringInterpolation.

struct ContentView: View {
    @Environment(.accessibilityShowButtonShapes)
    private var accessibilityShowButtonShapes
    
    var body: some View {
        Text("""
        (
            Text("[Visit our website](https://example.com)")
                .underline(accessibilityShowButtonShapes)
        ) to learn more about our products.
        """)
        .multilineTextAlignment(.center)
    }
}

If you enjoy our blog and would like to support us, you can Sponsor us on GitHub.

For blog updates and development tips, follow us on Twitter @nilcoalescing.

Source

Popular Articles