Chapters

Hide chapters

SwiftUI Cookbook

Live Edition · iOS 16.4 · Swift 5.8.1 · Xcode 14.3.1

Create an iOS & iPadOS App with SwiftUI
Written by Team Kodeco

SwiftUI provides a potent and user-friendly framework to craft captivating user interfaces across multiple Apple platforms. In this guide, you’ll become familiar with the essentials of developing an app that runs on iOS and iPadOS using SwiftUI.

Highlight your project file in the project navigator and make sure that iPad is added as a supported destination. Here’s what Xcode should look like:

iPad is a supported destination by default when starting from an iOS app template.
iPad is a supported destination by default when starting from an iOS app template.

Next change the simulator to an iPad device and open ContentView.swift. To incorporate a Text view that displays a greeting message, try the following code:

struct ContentView: View {
  var body: some View {
    Text("Hello, World!")
      .font(.largeTitle)
      .foregroundColor(.blue)
  }
}

Here’s what your preview should look like:

An iOS and iPadOS app in SwiftUI.
An iOS and iPadOS app in SwiftUI.

In this code snippet, you design a ContentView struct that complies with the View protocol. Within the body property, you employ the Text view to exhibit a greeting message. You then modify the font size to use the large title text style and color it blue.

Once your user interface is in place, running the app on a simulator or device is as easy as clicking the Run button in Xcode. This action builds and installs the app on the chosen platform, enabling you to examine your user interface and interact with it.

And that’s it! Creating an app that runs on both iOS & iPadOS with SwiftUI is an enjoyable and efficient experience that empowers you to develop high-quality and engaging user interfaces for Apple devices. With SwiftUI’s robust features and intuitive syntax, you are set to create dynamic and engaging apps that will thrill your users.

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.