The final screen in our app is CheckoutView
, and it’s really a tale of two halves: the first half is the basic user interface, which should provide little real challenge for you; but the second half is all new: we need to encode our Order
class to JSON, send it over the internet, and get a response.
We’re going to look at the whole encoding and transferring chunk of work soon enough, but first let’s tackle the easy part: giving CheckoutView
a user interface. More specifically, we’re going to create a ScrollView
with an image, the total price of their order, and a Place Order button to kick off the networking.
For the image, I’ve uploaded a cupcake image to my server that we’ll load remotely with AsyncImage
– we could store it in the app, but having a remote image means we can dynamically switch it out for seasonal alternatives and promotions.
As for the order cost, we don’t actually have any pricing for our cupcakes in our data, so we can just invent one – it’s not like we’re actually going to be charging people here. The pricing we’re going to use is as follows:
We can wrap all that logic up in a new computed property for Order
, like this:
var cost: Decimal {
// $2 per cake
var cost = Decimal(quantity) * 2
// complicated cakes cost more
cost += Decimal(type) / 2
// $1/cake for extra frosting
if extraFrosting {
cost += Decimal(quantity)
}
// $0.50/cake for sprinkles
if addSprinkles {
cost += Decimal(quantity) / 2
}
return cost
}
The actual view itself is straightforward: we’ll use a VStack
inside a vertical ScrollView
, then our image, the cost text, and button to place the order.
We’ll be filling in the button’s action in a minute, but first let’s get the basic layout done – replace the existing body
of CheckoutView
with this:
ScrollView {
VStack {
AsyncImage(url: URL(string: "https://hws.dev/img/cupcakes@3x.jpg"), scale: 3) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
ProgressView()
}
.frame(height: 233)
Text("Your total is \(order.cost, format: .currency(code: "USD"))")
.font(.title)
Button("Place Order", action: { })
.padding()
}
}
.navigationTitle("Check out")
.navigationBarTitleDisplayMode(.inline)
That should all be old news for you by now, but before we're done with this screen I want to show you a small but useful SwiftUI modifier we can add here: scrollBounceBehavior()
.
Using scroll views is a great way to make sure your layouts work great no matter what Dynamic Type size the user has enabled, but it creates a small annoyance: when your views fit just fine on a single screen, they still bounce a little when the user moves up and down on them.
The scrollBounceBehavior()
modifier helps us disable that bounce when there is nothing to scroll. Add this below navigationBarTitleDisplayMode()
:
.scrollBounceBehavior(.basedOnSize)
With that in place we'll get nice scroll bouncing when we have actually scrolling content, otherwise the scroll view acts like it isn't even there.
With that last tweak out of the way, it's time to finish up this project by tackling the tricky part: networking!
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.