Getting started with SwiftUI
During the WWDC19, Apple introduced a new UI framework : SwiftUI Apple described SwiftUI to be:
- Declarative, we say what we want.
- Consistant, there is no old APIs mixed up with new ones.
- Automatic, it does what we had to do ourself before.
- Compositional, we build this apart and then combine them.
The code is reduced, he’s now about 20% of what we had before in UIKit.
How does it works?
The are two big differences between
Viewis a protocol,
UIViewis a class.
- You’ll always need to return a view to render.
Here’s the base code when you ceate an app with SwiftUI
You can see that:
- It returns
UILabelin UIKit is
View always returns 1 thing. If you want to display multiple labels, you’ll need to place them inside a container.
As you can see, the view on the iPhone updates in live when we edit code.
And vice versa...
When we add something in the preview on the iPhone, it updates the code
Here are the tree containers that remplace
ZStackyes, Z. This displays content in top of each other.
The stacks take up only the space they need. If you want to add some space, add:
Space()between to items, they’ll split up to a side of the screen
Divider(), a little space will apear between the two views.
Adding Text is very simple:
This is all you need!
Know that you can cusomize your text like that:
and many others…
What is good to know is that you can add items from both code and preview.
cmd and clicking on you item in code or preview offers you a lot of possibilities like embed in stack.
It’s very close of UIKit.
I’m not planning to do a lot of tutorials on SwiftUI. Apple did a lot of tutorials here
You can find also a lot on the web. I was just presenting and introducing SwiftUI.
But, if you want me to do some tutorials, let me know and contact me!
If you liked this post, share it with your friends or should you have any feedback, contact me