An intro to SwiftUI
SwiftUI is a framework released by apple at WWDC2019, a developer convention for all apple platforms. SwiftUI is a declarative framework, therefore it uses Structs instead of classes. Lets dive into the framework itself with a basic view that contains a formatted label.
struct ContentView : View { var body: some View { VStack(alignment: .center){ Text("Welcome!") .font(.largeTitle) .bold() .color(.red) } } }
The beauty of SwiftUI is how much less code you have to type to get a simple view, and how clean the syntax is. In SwiftUI structs have to conform to :View just like when parsing JSON data structs have to conform too :Codable. The function bellow called body is the equivalent of the View displayed in Storyboards inside of ViewController. Lets get into some layout types. In SwiftUI there many types of UI layouts, however today I will only explain HStack (Horizontal Stack), and VStack (Vertical Stack). Inside of VStack we have a Text element commonly known as a label. Text(“Insert text”) is the declaration of a label. In the lines bellow that we can simply type a period to call all the options of Text. For example .Font(.largeTitle) makes the text “Insert text” larger in size. This gets rid of manuals having to change text size. SwiftUI handles device accessibility font size for you, which can be a problem when using UIKit because you may not have enabled dynamic font sizes. To set the font style of your Text label simply call forth .bold(), .italic(), etc. That was very simple, now lets set the text color. No more UIColor stuff, just call .color(.colorname) for example .color(.red). The color method can be called on all kinds of UI items such as Buttons, Views, etc.
Here is a picture of how our code looks in a simulator.
If you have any questions about this article press the Twitter icon bellow to go to my Twitter page.