Introduction to SwiftUI

“Better apps. Less code.”

Pınar Koçak
3 min readDec 9, 2022

“Better apps. Less code”, is that really true?

In this article, we are going to talk about SwiftUI which is a framework introduced to use of developers by Apple in 2019. I would like to make the entry with Federighi’s interview quote.

Apple’s SVP of software engineering, Craig Federighi elaborated on the goal of the SwiftUI:

Be able to write in the most minimal, clear, readable, expressable way, what you want the interface to be and have the system infer, okay how am I going to get that done? If the UI changes, if the underlying state changes such that the UI contents changes from this to that, handle the animation automatically…

Because we don’t have the developer overexpress themselves when creating the interface in the firstplace, all that’s there are the semantics and we can infer all the details and build the right interface for you. So the experience ends up being so much more fluid.

You can reach the detail of Federighi’s interview right here if you want.

Let’s talk about it’s advantages.

Preview is the most important advantage of SwiftUI. We can see every code changes with Preview in real time.

It saves developers time by providing interface design, Dark Mode, Accessibility, right-to-left language support and many more automatic functions, among the basic conveniences provided by SwiftUI. Applications developed with SwiftUI will run very fast as they will have many features built-in, which will provide a competitive advantage.

Also, SwiftUI gives us an chance to produce product with single design for all Apple products. To be more specific, I can say that with SwiftUI, you can offer the same design to WatchOS, iPadOs, MacOS and iOS at the same time.

In SwiftUI, the ViewControllers that we are used to, are completely disabled. There is no storyboard either. Saying goodbye to the tableViews which we use a lot. In other words, SwiftUI is a different world. We have Views now. Inside of that Views, we have body. But this allows us to create a much more structured and much nicer user interface.

In SwiftUI, Swift codes that we are familiar with, are still with us.

Professional iOS developers haven’t used mainStoryboard etc. until now. They have chosen to make UI with coding. But this choice seems to change with SwiftUI.

Except the advantages of SwiftUI, we have to say that there are disadvantages too:

  • It supports iOS 13 and upper versions.
  • It’s community is not fully settled.

I think I’ve said enough about SwiftUI. Now I will create a small project and touch on some important points through it.

Body can only contain a View. In this reason, definition that shown as below can be caused a problem. In this definition, you can only see the first item in the screen and also there are more than one Content View.

For solution of this problem, you can use VStack or HStack. In here, using VStack, we gathered all Views in one place. You can also stack Views with ZStack, but we didn’t need it here.

One of the best advantages of SwiftUI is that it enables us to work more efficiently on different screen sizes. We can achieve this with UIScreen.main.bounds. For example;

That’s all I’m going to talk about in this article. In my next article, I will talk about SwiftUI more deeper.

I hope you liked the article, please stay tuned for the rest of the article.

If you interested in Jetpack Compose in Android, I recommend you to check Ecem Okan’s article.

--

--