Coding Studio

Learn & Grow together.

iOS Core UI Development with SwiftUI (Complete Beginner Guide)

Modern iOS apps are built with powerful UI frameworks that make development faster, cleaner, and more maintainable. One of the biggest innovations introduced by Apple is SwiftUI, a declarative UI framework used to build user interfaces across iPhone, iPad, Mac, and Apple Watch.

In this guide, we will explore the core UI development concepts in SwiftUI, including views, layouts, navigation, state management, and architecture patterns like MVVM.


What is SwiftUI

SwiftUI is a modern UI framework introduced by Apple during Apple Worldwide Developers Conference 2019.

It allows developers to create user interfaces using declarative syntax in the Swift programming language.

Traditional UI Development

Previously iOS developers used:

  • UIKit
  • Storyboards
  • AutoLayout constraints

These approaches required more boilerplate code and manual UI updates.

SwiftUI Approach

In SwiftUI you simply declare what the UI should look like, and the framework automatically updates the interface when data changes.

Example:

struct ContentView: View {
var body: some View {
Text("Hello, SwiftUI!")
}
}

This single line creates a label on the screen.

Key Benefits of SwiftUI

  • Declarative UI development
  • Less boilerplate code
  • Live preview in Xcode
  • Built-in animations
  • Dark mode support
  • Cross-platform UI

Views & Modifiers

In SwiftUI, everything on the screen is a View.

Examples of views:

  • Text
  • Image
  • Button
  • List
  • Stack layouts

Example:

Text("Welcome to iOS Development")

Modifiers

Modifiers are used to customize views.

Example:

Text("Welcome to SwiftUI")
.font(.title)
.foregroundColor(.blue)
.padding()

This code:

  • Sets font size
  • Changes text color
  • Adds padding

Common SwiftUI Modifiers

ModifierPurpose
.font()Change font size
.padding()Add spacing
.background()Add background
.foregroundColor()Change color
.cornerRadius()Rounded corners

Modifiers make UI design clean and chainable.


VStack, HStack, ZStack

SwiftUI provides stack-based layout containers.

These are used to arrange views vertically, horizontally, or layered.


VStack (Vertical Stack)

Arranges views vertically.

Example:

VStack {
Text("Name")
Text("Email")
Text("Phone")
}

Output:

Name
Email
Phone

HStack (Horizontal Stack)

Arranges views horizontally.

Example:

HStack {
Image(systemName: "person")
Text("Profile")
}

Output:

[icon] Profile

ZStack (Layered Stack)

Places views on top of each other.

Example:

ZStack {
Color.blue
Text("Welcome")
.foregroundColor(.white)
}

Useful for:

  • Backgrounds
  • Overlays
  • Cards
  • Custom UI designs

List & ScrollView

Displaying multiple items is a common requirement in mobile apps.

SwiftUI provides List and ScrollView components.


List

List is used for table-like data display.

Example:

List {
Text("Apple")
Text("Banana")
Text("Orange")
}

Output:

A scrollable table similar to settings screens.

Example with dynamic data:

let fruits = ["Apple", "Banana", "Mango"]List(fruits, id: \.self) { fruit in
Text(fruit)
}

ScrollView

ScrollView allows custom scrollable layouts.

Example:

ScrollView {
VStack {
ForEach(1...20, id: \.self) { index in
Text("Item \(index)")
}
}
}

Unlike List, it gives full control over layout design.


NavigationStack

Navigation between screens is handled using NavigationStack.

Example:

NavigationStack {
VStack {
NavigationLink("Go to Details") {
DetailView()
}
}
}

Create the destination view:

struct DetailView: View {
var body: some View {
Text("Detail Screen")
}
}

Features:

  • Push navigation
  • Navigation title
  • Toolbar items
  • Programmatic navigation

State Management (@State, @Binding)

State management is one of the most important concepts in SwiftUI.


@State

@State stores local view state.

Example:

struct CounterView: View {    @State private var count = 0    var body: some View {
VStack {
Text("Count: \(count)") Button("Increase") {
count += 1
}
}
}
}

Whenever count changes, the UI automatically updates.


@Binding

@Binding allows two-way data sharing between views.

Parent View:

struct ParentView: View {    @State private var isOn = false    var body: some View {
ChildView(isOn: $isOn)
}
}

Child View:

struct ChildView: View {    @Binding var isOn: Bool    var body: some View {
Toggle("Enable", isOn: $isOn)
}
}

This allows the child view to modify parent state.


MVVM with SwiftUI

The recommended architecture for SwiftUI apps is MVVM (Model–View–ViewModel).

Components

Model

  • Data structures
  • Business data

View

  • UI layer built with SwiftUI

ViewModel

  • Handles logic and data transformation

Example MVVM Structure

Model:

struct User {
let name: String
}

ViewModel:

class UserViewModel: ObservableObject {    @Published var user = User(name: "John")
}

View:

struct UserView: View {    @StateObject var viewModel = UserViewModel()    var body: some View {
Text(viewModel.user.name)
}
}

Benefits of MVVM:

  • Clean architecture
  • Separation of concerns
  • Easy testing
  • Scalable codebase

Dark Mode Support

One of the biggest advantages of SwiftUI is automatic Dark Mode support.

Dark Mode was introduced in iOS 13.

SwiftUI automatically adapts UI colors based on system appearance.

Example:

Text("Hello SwiftUI")
.foregroundColor(.primary)

primary color automatically switches between:

  • Black (Light Mode)
  • White (Dark Mode)

Custom Dark Mode Support

Color(.systemBackground)

This ensures your app UI works perfectly in:

  • Light Mode
  • Dark Mode

Conclusion

SwiftUI has completely transformed iOS UI development by making it faster, simpler, and more declarative.

With powerful features like:

  • Stack-based layouts
  • Built-in navigation
  • Automatic state updates
  • MVVM architecture
  • Native Dark Mode support

Developers can build modern iOS applications efficiently using the Swift language and tools like Xcode.

If you’re starting iOS development today, learning SwiftUI is one of the best skills you can invest in.

Leave a Reply

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