Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make cornerRadius on Button edges so that edges are not clipped in swiftUI

Tags:

ios

swiftui

I am experimenting with SwiftUI. I do not know how to get a rounded button with a borderWidth. Below is the code that I have tried.

Here is my code

struct ArrowButton : View {
    var body: some View {
        return Button(action: {}) {
            HStack {
                Text("SEE ALL")
                    .font(.headline)
                    .color(Color.init(red: 0.627, green: 0.776, blue: 0.965))
                Image(systemName: "arrow.right")
                    .font(Font.title.weight(.bold))
            }
        }
            .padding(.all, 9.0)
            .border(Color.init(red: 0.627, green: 0.776, blue: 0.965))
            .cornerRadius(5.0)
    }
}

And here is how my view looks enter image description here

What modifiers should i use to

  1. Increase the border width?
  2. Remove the clipping effect shown below?
like image 384
swift nub Avatar asked Jun 10 '19 00:06

swift nub


People also ask

How do you round the edges of a button in SwiftUI?

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.

How do you round UIView corners?

If you start with a regular UIView it has square corners. You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.


1 Answers

The View.border(:width:cornerRadius) method is not available in the final Xcode 11 version but you can achieve the same result by applying both cornerRadius & overlay modifiers:

Button(action: {}) {
    VStack {
        Image(systemName: "star.fill")
        Text("Hello world!")
    }
    .padding()
    .accentColor(Color(.systemRed))
    .background(Color(UIColor.systemRed.withAlphaComponent(0.4)))
    .cornerRadius(4.0)
    .overlay(
        RoundedRectangle(cornerRadius: 4).stroke(Color(.systemRed), lineWidth: 2)
    )
}

Result:

Button example using the overlay modifier

like image 147
ricardopereira Avatar answered Sep 22 '22 04:09

ricardopereira