Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SwiftUI Projects

You're reading from   SwiftUI Projects Build six real-world, cross-platform mobile applications using Swift, Xcode 12, and SwiftUI

Arrow left icon
Product type Paperback
Published in Dec 2020
Publisher Packt
ISBN-13 9781839214660
Length 410 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Craig Clayton Craig Clayton
Author Profile Icon Craig Clayton
Craig Clayton
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Chapter 1: SwiftUI Basics 2. Chapter 2: SwiftUI Watch Tour FREE CHAPTER 3. Chapter 3: NBA Draft – Watch App 4. Chapter 4: Car Order Form – Design 5. Chapter 5: Car Order Form – Data 6. Chapter 6: Financial App – Design 7. Chapter 7: Financial App – Core Data 8. Chapter 8: Shoe Point of Sale System – Design 9. Chapter 9: Shoe Point of Sale System – CloudKit 10. Chapter 10: Sports News App – Design 11. Chapter 11: Sports News App – Data 12. Other Books You May Enjoy

Shapes

In SwiftUI, we have five preset shapes that you can work with, and they are super easy to create. The Circle, Rectangle, Ellipse, and Capsule are all created the same way. Let's look at each one and stop when you get to Rounded Rectangle.

Circle

Open ShapesCircle and let's take a look at how we can create a circle:

struct ShapesCircle: View {
    var body: some View {
        Circle()
            .fill(Color.red)
            .frame(width: 50, height: 50)
    }
}

If you tap the Resume button on the left, you'll see the following:

Figure 1.12

Figure 1.12

Creating shapes is easy in SwiftUI; in the preceding example, our circle is filled with red and is 50x50 in size. We will use custom shapes to create our UI. We can now move on to Rectangle.

Rectangle

We are now going to take a look at the Rectangle. Open ShapesRectangle and in our next example, let's take a look at creating a basic rectangle:

struct ShapesRectangle: View {
    var body: some View {
        Rectangle()
            .fill(Color.red)
            .frame(width: 50, height: 50)
    }
}

Tap on the Resume button, and you'll see the following:

Figure 1.13

Figure 1.13

In this last example, our Rectangle is filled with red and is 50 x 50 in size. Let's move on to our next example.

Ellipse

We'll now take a look at an ellipse. Open ShapesEllipse, and you will see we applied the same red fill with a size of 100 x 50:

struct ShapesEllipse: View {
    var body: some View {
        Ellipse()
            .fill(Color.red)
            .frame(width: 100, height: 50)
    }
}

Tap the Resume button, and you'll see the following:

Figure 1.14

Figure 1.14

We created an Ellipse, and as you can see, the code is not changing – the shape making, for the most part, has the same pattern. Let's move to the Capsule next.

Capsule

We are onto the next to last shape, the Capsule. Capsules are handy for creating bar charts, which we do later in this book. Let's take a minute and look at a basic Capsule. Open up ShapesCapsule:

struct ShapesCapsule: View {
    var body: some View {
        Capsule()
            .fill(Color.red)
            .frame(width: 200, height: 50)
    }
}

Tap the Resume button, and you'll see the following:

Figure 1.15

Figure 1.15

We now have a capsule sitting in the center of the screen. We can now move on to the final shape, and that's the Rounded Rectangle.

Rounded Rectangle

The Rounded Rectangle is the only shape that has a parameter, .cornerRadius, when you create one. Let's open ShapesRoundedRectangle and check out our final shape:

struct ShapesRoundedRectangle: View {
    var body: some View {
        RoundedRectangle(cornerRadius: 25)
            .fill(Color.red)
            .frame(width: 200, height: 25)
    }
}

Tap the Resume button, and you'll see the following:

Figure 1.16

Figure 1.16

We are finished looking at shapes, but remember that all shapes by default have a fill color of black. Next, we'll focus on the view layout and presentation.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image