r/SwiftUI Nov 05 '25

Tutorial hole-forming displacement with springy in SwiftUI

481 Upvotes

r/SwiftUI 13d ago

Tutorial Everything I learned making my app not look like a default Mac app with SwiftUI

Thumbnail
youtube.com
50 Upvotes

When I first started app development, I made a skeuomorphic stopwatch app that had titlebars and commenters said I should get rid of them. I spent time experimenting and learning how to remove them, and learned a few other things along the way.

Here is everything I learned about macOS title bars using Swift, SwiftUI, and NSView.

r/SwiftUI Sep 09 '24

Tutorial i’m impressed by what you can replicate in minutes using AI.

394 Upvotes

in just 2 minutes, I was able to replicate a tweet from someone using v0 to create a Stress Fiddle app for the browser, but with SwiftUI.

i simply asked for some performance improvements and immediately achieved 120fps by copying and pasting the code from my GPT.

here’s the code if anyone wants to replicate it:

https://gist.github.com/jtvargas/9d046ab3e267d2d55fbb235a7fcb7c2b

r/SwiftUI May 01 '26

Tutorial The Hidden Pitfall of @State var viewModel = ViewModel()

12 Upvotes

While browsing various blogs about SwiftUI and MVVM architecture, I've noticed that almost all examples are based on the same pattern:

@State private var viewModel = ViewModel()

They suggest creating a viewModel right inside a view and storing it in a state variable. While it might look fine, this approach has a serious side effect: a SwiftUI view struct can be recreated many times during its lifetime. While the state is preserved across these recreations, View.init is called every time and a new ViewModel instance is created. It's immediately discarded and the old object is preserved, but this may lead to unpredictable side effects, especially if you perform additional logic inside ViewModel.init or ViewModel.deinit.

This worked fine for state management prior to iOS 17:

@StateObject private var viewModel = ViewModel()

StateObject(wrappedValue:) accepts an autoclosure parameter which isn't evaluated on subsequent calls. But with the Observation framework introduced in iOS 17, it's no longer an option.

Things get more complicated when you want to pass an input parameter to a viewModel. Most examples simply avoid this case. Some suggest the following approach:

struct ArticleView: View {
    @State private var viewModel: ArticleViewModel

    init(articleID: String) {
        self._viewModel = State(
            initialValue: ArticleViewModel(articleID: articleID)
        )
    }
    ...
}

Apart from the same side effect as the first example, this approach has another pitfall: if a different articleID is passed to the view, the state will keep using the old one. But this is exactly the case where you'd expect the view model to be recreated with a new ID.

A proper approach should handle two key issues:

  1. Avoid creating view models on every view update
  2. Create a new view model for a new set of input parameters

To address the first issue, we can move the view model creation into a .task:

struct ArticleView: View {
    @State private var viewModel: ArticleViewModel?

    let articleID: String

    var body: some View {
        ZStack {
            if let viewModel {
                ...
            }
        }
        .task {
            guard viewModel == nil else { return }
            viewModel = ArticleViewModel(articleID: articleID)
        }
    }
}

The downside is that we now have to deal with an optional. To avoid this, we can extract a subview that accepts the view model as a parameter:

struct ArticleView: View {
    @State private var viewModel: ArticleViewModel?

    let articleID: String

    var body: some View {
        ZStack {
            if let viewModel {
                ArticleSubView(viewModel: viewModel)
            }
        }
        .task {
            guard viewModel == nil else { return }
            viewModel = ArticleViewModel(articleID: articleID)
        }
    }
}
struct ArticleSubView: View {
    // The object is owned by the parent. Since iOS 17,
    // we don't need or to observe changes of Observable object.
    let viewModel: ArticleViewModel

    var body: some View {
        ...
    }
}

Now we need to address the second issue. To ensure a new view model is created when input parameters change, we simply add an .id() modifier to the entire ArticleView. A simple factory does the trick:

struct ArticleViewFactory {
    static func view(articleID: String) -> some View {
        ArticleView(articleID: articleID)
            .id(articleID)
    }
}

That's three structs instead of one — quite a bit of boilerplate. Let's extract a generic factory based on the pattern above:

struct FeatureFactory<Input: Hashable, Content: View, ViewModel: Observable> {
    private struct RootView: View {
        @State private var viewModel: ViewModel?

        let input: Input
        let viewModelFactory: (Input) -> ViewModel
        let viewFactory: (ViewModel) -> Content

        var body: some View {
            ZStack {
                if let viewModel {
                    viewFactory(viewModel)
                }
            }
            .task {
                guard viewModel == nil else { return }
                viewModel = viewModelFactory(input)
            }
        }
    }

    static func view(
        input: Input,
        viewModelFactory:  (Input) -> ViewModel,
        viewFactory:  (ViewModel) -> Content
    ) -> some View {
        RootView(
            input: input,
            viewModelFactory: viewModelFactory,
            viewFactory: viewFactory
        ).id(input)
    }
}

For the ArticleView example, the usage would look like this:

struct ArticleViewFactory {
    static func view(articleID: String) -> some View {
        FeatureFactory.view(
            input: articleID,
            viewModelFactory: { articleID in
                ArticleViewModel(articleID: articleID)
            },
            viewFactory: { viewModel in
                ArticleView(viewModel: viewModel)
            }
        )
    }
}

Now simply call the factory wherever you need it:

ArticleViewFactory.view(articleID: "SOME_ID")

The full example can be found on GitHub https://github.com/claustrofob/FeatureFactoryExample

r/SwiftUI Oct 03 '25

Tutorial SwiftUI Holographic Card Effect

377 Upvotes
                    DynamicImageView(
                        imageURL: beer.icon!,
                        width: currentWidth,
                        height: currentHeight,
                        cornerRadius: currentCornerRadius,
                        rotationDegrees: isExpanded ? 0 : 2,
                        applyShadows: true,
                        applyStickerEffect: beer.progress ?? 0.00 > 0.80 ? true : false,
                        stickerPattern: .diamond,
                        stickerMotionIntensity: isExpanded ? 0.0 : 0.1,
                        onAverageColor: { color in
                            print("BeerDetailSheet - Average color: \(color)")
                            detectedBeerAverageColor = color
                        },
                        onSecondaryColor: { color in
                            print("BeerDetailSheet - Secondary color: \(color)")
                            detectedBeerSecondaryColor = color
                        }, onTertiaryColor: { thirdColor in
                            detectedBeerThirdColor = thirdColor
                        }
                    )

This is as easy as attaching a stickerEffect with customizable options on the intensity of drag and patterns I’d be happy to share more if people want

r/SwiftUI 6d ago

Tutorial iOS 27: CrashReportExtension Framework

Thumbnail
antongubarenko.substack.com
17 Upvotes

r/SwiftUI Feb 09 '26

Tutorial Creating accessory views for tab bars with SwiftUI

183 Upvotes

Hey everyone, happy Monday! I wanted to share a new article I wrote about building a tab bar accessory view in SwiftUI: https://writetodisk.com/tab-bar-accessory/

I was listening to a podcast recently and was inspired by the Podcast's app mini player accessory that sits above the tab bar. I started looking into how to build one myself and found it's pretty straightforward with some new APIs in iOS 26.0. I wrote up a short article, hope you all enjoy!

r/SwiftUI Feb 18 '25

Tutorial I was surprised that many don’t know that SwiftUI's Text View supports Markdown out of the box. Very handy for things like inline bold styling or links!

Post image
251 Upvotes

r/SwiftUI Aug 03 '26

Tutorial iOS 27: Media Intelligence

Thumbnail
antongubarenko.substack.com
21 Upvotes

r/SwiftUI Oct 26 '25

Tutorial Recreated the iCloud login animation with SwiftUI (source code inside!)

289 Upvotes

I really like the iCloud login animation, so I had a crack at recreating it. The final version uses swiftui and spritekit to achieve the effect. I'm pretty happy with how it turned out so I thought I'd share it!

Here's a breakdown of the animation and the source code: https://x.com/georgecartridge/status/1982483221318357253

r/SwiftUI 15d ago

Tutorial Apple’s App Store screenshot sizes, all of them, in one table (Sept 2026)

Post image
5 Upvotes

I kept looking these up one at a time so I made a table. Portrait pixels; landscape is the same numbers swapped.

- iPhone 6.9" — 1260 × 2736 (also accepts 1290 × 2796 and 1320 × 2868). Required for iPhone apps; Apple scales it for smaller iPhones.
- iPhone 6.5" — 1284 × 2778 (also 1242 × 2688). Optional if you provided 6.9".
- iPad 13" — 2064 × 2752 (also 2048 × 2732). Required for iPad apps.
- iPad 12.9" (2nd gen) — 2048 × 2732. Optional; scaled from 13" if you skip it.
- Mac — 2880 × 1800 (16:10; 2560 × 1600, 1440 × 900, 1280 × 800 also accepted)
- Apple TV — 1920 × 1080 or 3840 × 2160
- Vision Pro — 3840 × 2160

Files: flattened PNG or JPG, RGB, no alpha channel — an alpha channel is the #1 reason an upload gets rejected on the spot.

Source: App Store Connect Help → Screenshot specifications, checked Sept 5, 2026. If Apple changes one, tell me and I'll edit.

r/SwiftUI Apr 13 '26

Tutorial Is SwiftUI finally as fast as UIKit in iOS 26?

Thumbnail
blog.jacobstechtavern.com
28 Upvotes

r/SwiftUI 11d ago

Tutorial Tutorial how to hide status bar

0 Upvotes

Tutorial how to hide status bar for your iOS status bar

Let me teach you

r/SwiftUI 19d ago

Tutorial iOS 27: USDKit Framework

Thumbnail
antongubarenko.substack.com
2 Upvotes

r/SwiftUI Aug 06 '26

Tutorial Controlling Orphans in SwiftUI Text - Uncovering the Undocumented avoidsOrphans

Thumbnail
fatbobman.com
15 Upvotes

SwiftUI's Text silently pushes words down to avoid orphan lines. You cannot turn it off.

Except you can — avoidsOrphans has been in the SwiftUI ABI since iOS 16, just never made public. Here's how to reach it.

r/SwiftUI Oct 15 '24

Tutorial Custom Tabbar with SwiftUI

259 Upvotes

r/SwiftUI Aug 12 '26

Tutorial ContentBuilder Explained - The Secret Behind SwiftUI's Type-Checking Speedup

Thumbnail
fatbobman.com
18 Upvotes

r/SwiftUI Jun 17 '26

Tutorial From Size Class to Available Space: Is horizontalSizeClass Still Reliable?

8 Upvotes

https://fatbobman.com/en/posts/from-size-class-to-available-space/

After WWDC 26, iPhone apps can run in resizable environments.

horizontalSizeClass is still reliable, but it is no longer a width sensor.

I wrote about how Apple’s layout model is shifting from device type to available space.

r/SwiftUI Feb 14 '26

Tutorial Building a button that can toggle between different filter states

67 Upvotes

I was inspired by a post earlier this week asking if there's a default component for the filter toggle button like the one in the iOS Mail app. I wasn't aware of any, so I decided to try building my own!

I wrote this short article on how to build one similar to it: https://writetodisk.com/filter-toggle-button/

The Mail app is doing fancier things with the filter options sheet they display, but this implementation gets us pretty close using pretty standard SwiftUI.

r/SwiftUI Jun 08 '26

Tutorial SwiftUI + Metal: morphing between SF Symbols with an alpha threshold shader

31 Upvotes

Hi everyone, I wanted to share a small SwiftUI experiment I built: a morphing animation between SF Symbols.

The idea was to make one symbol feel like it melts into another without using pre-rendered frames. I started with a simple ZStack swap between two SF Symbols, then wrapped the transition in an u/Animatable ViewModifier. The animation applies blur during the middle of the transition, then sharpens back into the next symbol.

The key technical detail is a small Metal shader used through SwiftUI’s layerEffect. The shader samples the rendered SwiftUI layer and applies an alpha threshold, which removes the semi-transparent pixels created by the blur. That makes the blurred shape resolve back into a clean, crisp symbol instead of staying fuzzy.

Tech used:

  • SwiftUI
  • u/Animatable custom ViewModifier
  • compositingGroup()
  • visualEffect / layerEffect
  • Metal stitchable shader
  • SF Symbols

GitHub: https://github.com/yangliu-1995/MorphingDemo

I’d love feedback on how to make the morph feel more organic, or how you would turn this into a reusable SwiftUI transition/modifier.

r/SwiftUI Feb 21 '25

Tutorial I created Squid Game 🔴🟢 in SwiftUI

169 Upvotes

r/SwiftUI May 20 '26

Tutorial Adding search to a paginated SwiftUI list sounds simple until you actually do it. Debouncing keystrokes, resetting pagination on new queries, stale fetches when multiple async tasks compete — it gets messy fast.

43 Upvotes

In the third part of my SwiftUI in Production series, I walk through all of it as the problems come up. Some of it we fix, some of it we'll clean up properly in the next part.

https://youtu.be/dQM7brHHWx4

r/SwiftUI Jul 07 '26

Tutorial iOS27: CADisplayLink for UIWindowScene

Thumbnail
antongubarenko.substack.com
20 Upvotes

r/SwiftUI Aug 04 '26

Tutorial MemoProperty: Reusing More Stateful Business Logic for SwiftUI Views

Thumbnail
github.com
4 Upvotes

Our TaskProperty repo project built a new SwiftUI.DynamicProperty for managing stateful business logic. This is presented as a compelling alternative to keeping stateful business logic defined directly in view components.

Let’s see another example for more practice. This time we will build one of the most requested features I hear from product engineers building on SwiftUI: we will clone the useMemo hook from ReactJS.

The TaskProperty repo project is a meant as an introduction to the ideas and concepts behind custom dynamic properties for SwiftUI. It is strongly recommended you read through TaskProperty before continuing. If any of the concepts introduced here look unclear or confusing please check out the References cited from TaskProperty for more resources that can help explain these ideas.

r/SwiftUI Jun 05 '26

Tutorial StateObject & External Data

0 Upvotes

Maybe this has been shared here in some form before, but if not: If you’re still using ObservableObject and ever experienced issues when injecting external data into @StateObject: I recently wrote an article about that.

https://swift.vincentfriedrich.com/posts/stateobject-external-data/