Hamdouchi Interactive
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk

Unlocking the Power of SwiftUI:
A Tech Blog Series

Welcome to our blog series focused on the exciting world of SwiftUI! Here, we aim to share valuable insights, practical tips, and deep dives into the latest SwiftUI features and techniques. From crafting seamless user interfaces to exploring performance strategies, our articles are designed to inspire and empower developers of all levels. Stay connected as we explore the endless possibilities with SwiftUI and spark engaging discussions about building modern, efficient apps.

Explore all SwiftUI Topics

SwiftUI Basics: Views, Modifiers, and State Management

1/19/2025

0 Comments

 
Picture
Welcome back to Week 3 of the SwiftUI Blog Series! In the first two weeks, we explored why SwiftUI is a revolutionary framework and built our first SwiftUI app. This week, we’ll dig deeper into the essential building blocks of SwiftUI: Views, Modifiers, and State Management. These concepts are fundamental to creating dynamic, interactive, and beautiful apps.​

What Are Views in SwiftUI?

​At its core, SwiftUI uses Views to define what users see on the screen. Views are lightweight, reusable, and describe the appearance of your app’s user interface. Unlike UIKit, where views are objects, SwiftUI views are immutable value types.
Key Characteristics of Views
​

  1. Declarative: You define the UI by describing what it should look like.
  2. Composable: Views are small and modular, making it easy to combine them into complex layouts.
  3. Lightweight: Views only describe the UI — they don’t hold or manage state.
Example: A Simple View

    
​Customizing Views with Modifiers
Here are some examples of how modifiers can transform views
  • Text Modifiers​

    
Picture
  • Background and Borders

    
Picture
  • Image Modifiers

    
Picture
Modifier Order Matters

​The order in which you apply modifiers affects the result. For example:

    
Picture
​Reversing the order will produce a different layout, so keep an eye on your modifier chains!

​State Management in SwiftUI

​State is the driving force behind dynamic and interactive UIs in SwiftUI. It represents the data that the view depends on, and when state changes, SwiftUI automatically updates the UI.
Using @State
​
​The @State property wrapper allows SwiftUI to manage changes to a variable and re-render the affected views.
Example: A Counter App

    
Counter
Key Points:
  • @State variables are local to the view they’re declared in.
  • SwiftUI automatically re-renders the UI when the state changes.

Sharing State Between Views

When you need to share state between a parent and a child view, you can use the @Binding property wrapper.

Using @Binding

​The parent view owns the state, and the child view uses a binding to access and modify it.

    
Practical Example: A Toggle Switch App

Let’s bring everything together in a practical example:

    
Picture
How It Works:
  • The @State property isOn manages the switch’s state.
  • The Toggle view updates isOn when the switch is toggled.
  • The Text view dynamically updates based on the state of isOn.​

Key Takeaways
  1. Views: The foundation of SwiftUI, used to define the user interface.
  2. Modifiers: Tools for customizing the appearance and behavior of views.
  3. State Management: The mechanism that keeps your UI in sync with your app’s data.

By mastering these basics, you’re building a solid foundation for more advanced SwiftUI concepts.
What’s Next?

​In Week 4, we’ll explore SwiftUI Previews, a powerful tool for visualizing and testing your app in real-time. You’ll learn how to maximize productivity and iterate faster with this feature.

Stay tuned and keep coding!
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Picture

    ​Mohamed Hamdouchi


    Author

    Lead iOS Engineer.
    I help develop Design System Libraries.
    Creative Thinker.
    Featured on the App Store with 4M+ downloads.

    Archives

    March 2025
    February 2025
    January 2025

    Categories

    All
    Alignment
    Binding
    EnvironmentObject
    Frames
    Grids
    Layout
    List
    Modifiers
    ObservedObject
    Overlay
    Previews
    Responsive Design
    Stacks
    State
    State Management
    StateObject
    Task Manager
    Views
    ZStack

​​COPYRIGHT © 2009 HAMDOUCHI INTERACTIVE, LLC. ​ALL RIGHTS RESERVED
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk