how should i create a border around a swiftui button?

Generated on 8/7/2024

1 search

To create a border around a SwiftUI button, you can use the border modifier. Here's a simple example:

Button(action: {
    // Your action here
}) {
    Text("Press me")
}
.border(Color.blue, width: 2)

This code creates a button with a blue border that is 2 points wide.

For more advanced styling, you can use different button styles provided by SwiftUI. For instance, in the session Migrate your TVML app to SwiftUI, it is mentioned that buttons on tvOS use the bordered button style by default, but you can also use the borderless button style for a different appearance.

If you want to learn more about button styles and their adaptivity across different contexts, you can refer to the session SwiftUI essentials.

For creating custom hover effects and more advanced button interactions, you might find the session Create custom hover effects in visionOS useful.