swiftui - How to round specific corners of a View?

Swiftui - How to round specific corners of a View?

In SwiftUI, you can round specific corners of a View using the cornerRadius() modifier along with a custom Shape that defines which corners should be rounded. Here's how you can achieve this:

Example: Round Specific Corners of a View

In this example, we'll create a custom Shape called RoundedCorners to specify which corners of a Rectangle should be rounded. Then, we'll apply this custom shape to a Rectangle view.

import SwiftUI struct ContentView: View { var body: some View { Rectangle() .fill(Color.blue) .frame(width: 200, height: 100) .clipShape(RoundedCorners(corners: [.topLeft, .bottomRight], radius: 20)) } } struct RoundedCorners: Shape { var corners: UIRectCorner var radius: CGFloat func path(in rect: CGRect) -> Path { let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) return Path(path.cgPath) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } 

Explanation:

  1. Custom Shape (RoundedCorners):

    • RoundedCorners conforms to the Shape protocol, which allows us to define a custom path.
    • corners specifies which corners of the rectangle should be rounded ([.topLeft, .bottomRight] in this example).
    • radius sets the radius of the rounded corners.
  2. Applying the Custom Shape:

    • Inside ContentView, a Rectangle view is created.
    • .clipShape(RoundedCorners(...)) applies the custom RoundedCorners shape to clip the Rectangle and round specific corners.
  3. Preview:

    • ContentView_Previews provides a preview of ContentView.

Alternative Approach Using cornerRadius() Modifier

If you want to round all corners of a Rectangle and then overlay another view to mask specific corners, you can use overlay and mask modifiers. Here's an example:

struct ContentView: View { var body: some View { Rectangle() .fill(Color.blue) .frame(width: 200, height: 100) .cornerRadius(20) .overlay( Rectangle() .fill(Color.white) .frame(width: 200, height: 100) .cornerRadius(20) .mask(RoundedCorners(corners: [.topLeft, .bottomRight], radius: 20)) ) } } 

Explanation:

  • cornerRadius(): Initially rounds all corners of the Rectangle.
  • overlay(): Overlays another Rectangle to mask specific corners.
  • .mask(RoundedCorners(...)): Applies the custom RoundedCorners shape as a mask to the overlay, effectively masking the specified corners.

Conclusion

Using either the clipShape or overlay + mask approach allows you to round specific corners of a View in SwiftUI. Choose the method that best fits your layout and design requirements. SwiftUI's declarative syntax and modifiers provide a flexible way to customize UI elements efficiently.

Examples

  1. How to round specific corners of a SwiftUI View in Swift?

    • Description: Rounding specific corners of a SwiftUI View using cornerRadius and clipShape.
    • Code:
      struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .padding() .background(Color.blue) .cornerRadius(10, corners: [.topLeft, .bottomRight]) } } extension View { func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View { clipShape(RoundedCorners(radius: radius, corners: corners)) } } struct RoundedCorners: Shape { var radius: CGFloat = .infinity var corners: UIRectCorner = .allCorners func path(in rect: CGRect) -> Path { let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) return Path(path.cgPath) } } 
  2. How to round top corners of a SwiftUI View in Swift?

    • Description: Rounding only the top corners of a SwiftUI View using extensions and clipShape.
    • Code:
      struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .padding() .background(Color.blue) .cornerRadius(10, corners: [.topLeft, .topRight]) } } // Extension and RoundedCorners implementation from previous example 
  3. How to round bottom corners of a SwiftUI View in Swift?

    • Description: Applying corner radius to only the bottom corners of a SwiftUI View.
    • Code:
      struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .padding() .background(Color.blue) .cornerRadius(10, corners: [.bottomLeft, .bottomRight]) } } // Extension and RoundedCorners implementation from previous example 
  4. How to create a circular shape for a SwiftUI View in Swift?

    • Description: Creating a circular shape for a SwiftUI View using Circle shape.
    • Code:
      struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .padding() .background(Color.blue) .clipShape(Circle()) } } 
  5. How to round all corners of a SwiftUI View uniformly in Swift?

    • Description: Applying a uniform corner radius to all corners of a SwiftUI View.
    • Code:
      struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .padding() .background(Color.blue) .cornerRadius(10) } } 
  6. How to round specific corners with different radii in SwiftUI?

    • Description: Setting different corner radii for specific corners of a SwiftUI View.
    • Code:
      struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .padding() .background(Color.blue) .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous)) .mask(RoundedCorners(radius: 10, corners: [.topLeft, .bottomRight])) } } struct RoundedCorners: Shape { var radius: CGFloat = .infinity var corners: UIRectCorner = .allCorners func path(in rect: CGRect) -> Path { let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) return Path(path.cgPath) } } 
  7. How to round specific corners of a rectangle in SwiftUI?

    • Description: Applying corner radius to specific corners of a rectangle shape in SwiftUI.
    • Code:
      struct ContentView: View { var body: some View { RoundedRectangle(cornerRadius: 10, style: .continuous) .frame(width: 200, height: 100) .foregroundColor(.blue) .overlay( Text("Hello, SwiftUI!") .foregroundColor(.white) ) .mask(RoundedCorners(radius: 10, corners: [.topLeft, .bottomRight])) } } struct RoundedCorners: Shape { var radius: CGFloat = .infinity var corners: UIRectCorner = .allCorners func path(in rect: CGRect) -> Path { let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) return Path(path.cgPath) } } 
  8. How to round specific corners of an image in SwiftUI?

    • Description: Applying corner radius to specific corners of an image view in SwiftUI.
    • Code:
      struct ContentView: View { var body: some View { Image("exampleImage") .resizable() .frame(width: 200, height: 200) .clipShape(RoundedCorners(radius: 20, corners: [.topLeft, .bottomRight])) } } struct RoundedCorners: Shape { var radius: CGFloat = .infinity var corners: UIRectCorner = .allCorners func path(in rect: CGRect) -> Path { let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) return Path(path.cgPath) } } 
  9. How to round specific corners of a button in SwiftUI?

    • Description: Applying corner radius to specific corners of a button view in SwiftUI.
    • Code:
      struct ContentView: View { var body: some View { Button(action: { // Button action }) { Text("Tap Me") .padding() .foregroundColor(.white) .background(Color.blue) .clipShape(RoundedCorners(radius: 10, corners: [.topLeft, .bottomRight])) } } } struct RoundedCorners: Shape { var radius: CGFloat = .infinity var corners: UIRectCorner = .allCorners func path(in rect: CGRect) -> Path { let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) return Path(path.cgPath) } } 
  10. How to round specific corners of a text view in SwiftUI?

    • Description: Applying corner radius to specific corners of a text view in SwiftUI.
    • Code:
      struct ContentView: View { var body: some View { Text("Hello, SwiftUI!") .padding() .foregroundColor(.white) .background(Color.blue) .clipShape(RoundedCorners(radius: 10, corners: [.topLeft, .bottomRight])) } } struct RoundedCorners: Shape { var radius: CGFloat = .infinity var corners: UIRectCorner = .allCorners func path(in rect: CGRect) -> Path { let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) return Path(path.cgPath) } } 

More Tags

aspose pyspark actionresult lastinsertid graphicsmagick java-http-client google-polyline checkmarx tablecell message-listener

More Programming Questions

More Electronics Circuits Calculators

More Housing Building Calculators

More Math Calculators

More Animal pregnancy Calculators