SwiftUI Basic Shapes Tutorial

In SwiftUI basic shapes can be drawn like a Rectangle or a Circle. In this tutorial some shapes will be drawn with different modifiers. SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be downloaded at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select the Single View App template, and then click Next. Enter SwiftUIBasicShapesTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next. Choose a location to save the project on your Mac.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select ContentView.swift. Change the code inside the ContentView struct to

struct ContentView: View {
    var body: some View {
        VStack {
            // 1.
                .stroke(Color.blue, lineWidth: 10)
                .frame(width: 100, height: 100)
            // 2. 
                .frame(width: 100, height: 100)
            // 3.
                        .stroke(Color.black, lineWidth: 10)
                .frame(width: 200, height: 100)
            // 4.
            RoundedRectangle(cornerRadius: 20)
                .frame(width: 100, height: 100)
  1. A rectangle is drawn with a outline

  2. A circle is drawn with a red fill color

  3. A filled capsule is drawn with a stroke outline

  4. A rounded rectangle is drawn with a corner radius of 20 degrees.

The preview will look like this.

The source code of the SwiftUIBasicShapesTutorial can be downloaded at the ioscreator repository on Github.