If we ask for it, iOS allows us to place content anywhere on the screen, including under the system clock at the top and the home indicator at the bottom. This doesn’t look great, which is why by default SwiftUI ensures components are placed in an area where they can’t be covered up by system UI or device rounded corners – an area known as the safe area.
On an iPhone 15, the safe area spans the space from just below the dynamic island down to just above the home indicator. You can see it in action with a user interface like this one:
struct ContentView: View {
var body: some View {
Form {
Section {
Text("Hello, world!")
}
}
}
}
Try running that in the iOS simulator – press the Play button in the top-left corner of Xcode’s window, or press Cmd+R.
You’ll see that the form starts below the dynamic island, so by default the row in our form is fully visible. However, forms can also scroll, so if you swipe around in the simulator you’ll find you can move the row up so it goes under the clock, making them both hard to read.
A common way of fixing this is by placing a navigation bar at the top of the screen. Navigation bars can have titles and buttons, and in SwiftUI they also give us the ability to display new views when the user performs an action.
We’ll get to buttons and new views in a later project, but I do at least want to show you how to add a navigation bar and give it a title, because it makes our form look better when it scrolls.
You’ve seen that we can place a text view inside a section by adding Section
around the text view, and that we can place the section inside a Form
in a similar way. Well, we add a navigation bar in just the same way, except here it’s called NavigationStack
.
var body: some View {
NavigationStack {
Form {
Section {
Text("Hello, world!")
}
}
}
}
That will look identical, but usually want to put some sort of title in the navigation bar, and you can do that by attaching a modifier to whatever you’ve placed inside.
Let’s try adding a modifier to set the navigation title for our form:
NavigationStack {
Form {
Section {
Text("Hello, world!")
}
}
.navigationTitle("SwiftUI")
}
When we attach the .navigationTitle()
modifier to our form, Swift actually creates a new form that has a navigation title plus all the existing contents you provided.
When you add a title to a navigation bar, you’ll notice it uses a large font for that title. You can get a small font by adding another modifier:
.navigationBarTitleDisplayMode(.inline)
You can see how Apple uses these large and small titles in the Settings app: the first screen says “Settings” in large text, and subsequent screens show their titles in small text.
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.