How to Use Xcode Playgrounds to Create UI in Swift

To practice Swift programming and learn how to create and position UI elements programmatically in Swift you can use Xcode to create a new project or you can use Xcode Playgrounds. In this short tutorial, you will learn how to use Xcode Playgrounds to run Swift code that creates a new UIView with a single UILabel on it and displays results in a Live View panel.

Create a new Xcode Playground

To create a new Xcode playground you can either start Xcode and then choose File->New playground or when Xcode starts click on “Get started with Playground” option as it is illustrated in the image below:

Create new Xcode Playground

The next window Xcode will present you with will look the one on the image below. Select “Single View” and click Next.

Xcode Playgrounds. Create UIView.

 

Once you create a new Playground you will most likely see the following screen. It will already have an example of UIViewController created and ready for you to use. The next step will be to run this code.

Xcode Playgrounds. A new Playground.

Run Swift Code in Playground

When a new Playground is created you will see that it will already have a new UIViewController class created with a function that creates a new UIView with a single UILabel element on it. You can use this code and run it as is to see how it works.

To run Swift code in Xcode playgrounds, click on the Run button which is located in the lower-left corner as it is illustrated in the image below.

Run Swift code in Playground

Once you click on the Run button and run the code, the layout will change and the results will be presented in a Live View on the right side. Which is pretty awesome because you did not even need to run an iPhone simulator to see the results. You can now work with this playground to create your own UI elements, layout them as needed, and practice creating and customizing UI elements in Swift programmatically.

Xcode Playground. Live View.

I hope this very short Swift tutorial was helpful to you. If you are interested in learning more about Swift and how to use it to build mobile apps for the iOS platforms have a look at the video courses below. These are step-by-step video lessons that might help you speed up your learning progress and take your Swift programming skills to a whole new level.


Leave a Reply

Your email address will not be published. Required fields are marked *