DEV Community

How to Make a Button with SwiftUI

Maegan Wilson on June 04, 2019

If you are trying to follow along, please know that SwiftUI requires XCode 11 and macOS 10.15, all of which is in developer beta right now. This p...
Collapse
 
nunez_giovanni profile image
Giovanni Nunez

Do you know how to add, say a border color and / or background color?

I decided to give swiftUI a shot. I just began learning UIKit and AppKit earlier this week 😅 so I am still trying to soak in all the differences from all 3 frameworks.

Collapse
 
maeganwilson_ profile image
Maegan Wilson

Here is the code sample:

Button(action: {print("Button was tapped")}) {
    Text("Tap")
}
.border(.blue, width: 3.0, cornerRadius: 10.0)
.padding(.all, 5.0)
Collapse
 
nightsquid7 profile image
Nightsquid7

Awesome thanks! For some reason the apple documentation for border was buried beneath a bunch of links and I missed it. Glad you posted though!

Thread Thread
 
maeganwilson_ profile image
Maegan Wilson

I have a hard time with the Apple Documentation, so I like to share when I find something like that!

Collapse
 
nunez_giovanni profile image
Giovanni Nunez

woohoo. thanks for the example :-)
I just realized you're also from Austin. Hello, and thanks, neighbor!

Thread Thread
 
maeganwilson_ profile image
Maegan Wilson

You're welcome!

Ah that's cool! Howdy!!

Collapse
 
maeganwilson_ profile image
Maegan Wilson • Edited

Yes to the border and border color! I'll put a gist together at lunch and link it here. Unfortunately, I'm still playing around with the background color stuff. That's been a little trickier for me to figure out.

Oh fun! Since I don't do any of this for work, I just try to focus on the new stuff and have fun. Hopefully, an app I write will make money some day.

Collapse
 
salcidogrijalva profile image
Salcido Grijalva

This looks so much like Flutter 😯

Collapse
 
nicowernli profile image
Nicolás Wernli

Yes, swiftui is exactly what flutter is but just for iOS. Also swift makes the code look slightly cleaner, but the idea is the same

Collapse
 
maeganwilson_ profile image
Maegan Wilson

It does look similar! I haven't used Flutter, but just browsing the docs they look pretty close.