Chapters

Hide chapters

SwiftUI Cookbook

Live Edition · iOS 16.4 · Swift 5.8.1 · Xcode 14.3.1

Clip Views in SwiftUI
Written by Team Kodeco

Have you ever wanted to create a view with a unique shape, like a rounded rectangle or a circle? SwiftUI provides an easy way to achieve this using view clipping.

You can think of view clipping like using a cookie cutter. The view is the dough, and the shape you apply is the cookie cutter. Whatever lies outside the shape’s boundaries gets cut off, and you’re left with a view in the shape of the cookie cutter.

In order to clip views in SwiftUI, you apply the clipShape modifier. For example, suppose you have a rectangular view and you want to clip it to a rounded shape. Here’s how you can do it:

struct ContentView: View {
  var body: some View {
    Text("Hello, ClipShape!")
      .font(.largeTitle)
      .padding()
      .background(Color.blue)
      .foregroundColor(.white)
      .clipShape(RoundedRectangle(cornerRadius: 20))
  }
}

The preview for this view should look as follows:

An example of clipping a view's shape in SwiftUI.
An example of clipping a view's shape in SwiftUI.

In this example, you have a Text view that displays the text “Hello, ClipShape!”. You apply various modifiers to style the text, set a blue background and make the text white.

The interesting part is the .clipShape(RoundedRectangle(cornerRadius: 20)) modifier. This clips the view to a rounded rectangle shape with a corner radius of 20.

The result is a rounded rectangular view with the text displayed inside it. The corners of the view are rounded, giving it a visually appealing appearance. Feel free to experiment with different corner radii to achieve your desired look.

Here’s what’s happening behind the scenes. Clipping a view means defining its visible region. In the earlier example, the clipShape modifier clips the view by applying a rounded rectangle shape. Only the portion of the view that lies within the rounded rectangle’s boundaries will be visible.

More View Clipping Examples

To further explore clipping, try the other shape types provided by SwiftUI, such as circles, ellipses, capsules and custom shapes. Here are a few more examples of interesting effects you can obtain with clipShape:

struct ContentView: View {
  var body: some View {
    VStack(spacing: 20) {
      Text("Circle")
        .frame(width: 200, height: 100)
        .font(.title)
        .padding()
        .background(.orange)
        .foregroundColor(.white)
        .clipShape(Circle())

      Text("Ellipse")
        .frame(width: 200, height: 100)
        .font(.title)
        .padding()
        .background(.green)
        .foregroundColor(.white)
        .clipShape(Ellipse())

      Text("Capsule")
        .frame(width: 200, height: 100)
        .font(.title)
        .padding()
        .background(.purple)
        .foregroundColor(.white)
        .clipShape(Capsule())

      Text("Custom")
        .frame(width: 200, height: 100)
        .font(.title)
        .padding()
        .background(.yellow)
        .foregroundColor(.black)
        .clipShape(CustomShape())
    }
  }
}

struct CustomShape: Shape {
  func path(in rect: CGRect) -> Path {
    var path = Path()
    path.move(to: CGPoint(x: rect.midX, y: rect.minY))
    path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
    path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
    path.closeSubpath()
    return path
  }
}

The preview for this view should look as follows:

More clipping examples in SwiftUI.
More clipping examples in SwiftUI.

In this example, we have a VStack containing multiple Text views, each with its own clipShape modifier.

The first Text view is clipped to a circle shape using Circle. The second Text view is clipped to an ellipse shape using Ellipse. The third Text view is clipped to a capsule shape using Capsule.

Finally, the fourth Text view demonstrates clipping to a custom shape. You define a CustomShape struct that conforms to the Shape protocol and implement the path(in rect: CGRect) method to describe the desired shape. In this case, you create a simple triangular shape.

By applying the clipShape modifier with different shape types, you can achieve various clipping effects, allowing for unique and creative designs.

Feel free to experiment with other shape types and customize the corner radius or dimensions of the shapes to fit your specific requirements. SwiftUI’s clipping capabilities provide a powerful tool for shaping and transforming views to create visually stunning user interfaces.

Note: View clipping in SwiftUI can potentially impact performance, particularly when applied to complex or nested views. Consider alternatives like adjusting the frame size to fit the desired visible area, using overlay and background modifiers, creating custom shapes with the Shape protocol or pre-rendering static content into images. Profile your app’s performance with tools like Xcode’s Instruments. Each solution has its own trade-offs — the most effective approach will depend on your app’s specific needs.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.