Seems like it should be simple.
Button(action: {
}){
ZStack{
Circle()
.frame(width: 100, height: 100)
.foregroundColor(.blue)
Text("Press me")
}
}
This gives me:
I can only click on the rectangle part. Also bonus points if you can point out why the circle is cut off
EDIT: Turns out this is an issue with macOs.
Issue with Buttons in SwiftUI on MacOS
EDIT 2: As Asmari mention below, you can use PlainButtonStyle:
var body: some View {
VStack{
Button(action: {
print("Pressed!")
}){
Text("Press me")
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}.buttonStyle(PlainButtonStyle())
}.frame(width: 300, height: 500)
}
}
or use a custom style:
struct BlueButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}
}
struct ContentView: View {
var body: some View {
VStack{
Button(action: {
print("Pressed!")
}){
Text("Press me")
}.buttonStyle(BlueButtonStyle())
}.frame(width: 300, height: 500)
}
}
Any SwiftUI view can have its corners rounded using the cornerRadius() modifier. This takes a simple value in points that controls how pronounced the rounding should be.
SwiftUI's button is similar to UIButton , except it's more flexible in terms of what content it shows and it uses a closure for its action rather than the old target/action system. To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") }
A Button is a type of control that performs an action when it is triggered. In SwiftUI, a Button typically requires a title text which is the text description of your button, and an action function that will handle an event action when triggered by the user.
Try this one:
import SwiftUI
struct ContentView: View {
var body: some View {
Button(action: {
print("Round Action")
}) {
Text("Press")
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Output wil be:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With