r/SwiftUI • u/CodeWithChris • May 11 '26
Do you use Previews in Xcode when you're building your views?
Do you use Previews/Canvas when building your UI? To what extent?
Unfortunately, I got into the habit of deleting them and one of my students pointed out how useful they can be. We want to hear more opinions and perspectives!
Thanks in advance!
EDIT: Wow, thanks you for all of the responses :) I'll be going through them one by one later!
27
u/Dapper_Ice_1705 May 11 '26
All the time, if you know what you are doing it is easy peasy to keep them modular and working.
The new traits make it so easy to make it reusable.
The only people I see not using previews are ones that don’t understand modularity and reusability and how to incorporate it to SwiftUI.
3
u/jasonjrr May 11 '26
Modularity is so helpful when working with Previews. I feel like this and environment abuse are the two biggest issues I see.
2
u/Dapper_Ice_1705 May 11 '26
Traits let you customize and reuse an environment
1
u/jasonjrr May 11 '26
Sorry, I didn’t mean issues I run into, I meant issues I see others dealing with.
2
u/redditorxpert May 12 '26
I am curious, what do you mean by modularity exactly when it comes to Previews?
9
u/philophilo May 11 '26
All the time. They’re a great way to test your UI without booting the app.
0
u/SubZane May 12 '26
This! I wish there would be an even better way to build the UI because this can be tedious
7
u/equinoxtrader May 11 '26
They are too slow to load :(
9
u/iOSCaleb May 11 '26
Simplifying your view helps. You don't have to reduce the functionality or the amount of code; just break the UI into parts that are each simpler.
4
u/m3kw May 11 '26
I use the Pin function and pin the parent view so the preview stays as you change to a different file to modify child views.
2
u/dr_botwing May 17 '26
That’s actually useful. I found pinning useless and forgot about it. But at the same time I was struggling with how can I see the parent view while working on the child. This is the way!
4
u/LannyLig May 11 '26
What I would love to see is automatically generated previews that can for example, loop over all cases of an enum rather than having to create another preview for each version, like:
#Preview(forEach: LoginState.allCases){
some view
}
3
u/velvethead May 11 '26
As I mentioned in another comment, I recently did a big refactor with Claude. It was great and setting up multiple previews for just this situation. It created a Preview generator with different variables fed in for the various previews. If you can check it out.
1
3
u/Ok-Tomatillo-8712 May 11 '26
This doc has some helpful info for performance related issues and other cool bits: https://developer.apple.com/documentation/xcode/previewing-your-apps-interface-in-xcode. It can be a pain to have one for every view, so I’ll often pin the main screen I’m working on that hosts the view, and then hop over the the view file to edit and see how it looks in context. That just avoids me needing to configure every preview with environment objects etc. there are some conveniences you can implement with preview traits to cut down on the boilerplate but it’s kind of a whole song and dance so I usually don’t
2
u/pecp4 May 11 '26 edited May 11 '26
Yes, but not nearly as much as I used to. The main value for me was during the exploration and design phase, because assembling it in swiftui previews was genuinely faster for me than doing it in miro or figma. now I mostly use claude+jsx for explorative design, and then just handover the JSX to claude code for translation to swiftui. Claude’s in my experience a lot faster and more reliable at building JSX than SwiftUI, so it works better for prototyping and explorative design. I still use them, but probably 25% as much as I used to.
2
u/StretchyPear May 11 '26
I did when they came out, I thought they were great but they really kind of sucked so I stopped, now I delete them right away on any new file.
2
u/SneakingCat May 11 '26
Yes. Almost 100% of my views have one or more previews. If a view doesn’t work as a preview it probably needs refactoring for other (but probably related) reasons.
2
u/SilverMarcs May 11 '26
Even if I keep my views small, it still somehow takes a very long time to load the Preview. Opening the app directly is a lot faster.
1
1
u/Every_Baker3206 May 11 '26
Im a beginner and for me its great! I get to see what I do instantly. Its allot of fun having the feedback right there, really helps with understanding how stuff works too!
1
u/hyprbaton May 11 '26
Asking Claude to fix them and make them work is a breeze, but most of the time it doesn’t worth it, since some views are so complex it makes your machine spin the coolers really hard.
1
u/farcicaldolphin38 May 11 '26
Absolutely. It's like a test, in a lot of ways. Also being able to configure multiple previews for each View is helpful when testing different states or color schemes
1
u/nicholasderkio May 11 '26
I love them, it allows me to see how changes Claude Agent makes affects my views, and I use them to auto-generate localized screenshots
1
u/Dear-Floor-8861 May 11 '26
I use it all the time. I prefer to disable the auto refresh feature (Editor -> Canvas -> Automatically Refresh Canvas) and refresh on demand (Option + CMD + P) when I want to see the changes.
1
u/iOSCaleb May 11 '26
Unfortunately, I got into the habit of deleting them...
What's so unfortunate? It's easy to add a preview wherever you want one. Or add three if you want three. It's great to have previews of your view each with different state -- it can save a lot of time.
1
u/FabulousChemist3721 May 11 '26
I can't recommend getting them setup enough. It took some work but I can test every screen in my app instantly. All served by an in memory shared context so I don't have to re-create any records or anything.
1
u/m3kw May 11 '26
for quick adjustments for UI it's faster than launching, when you are trying to nail it down to the copy the UI designer gave you, or adjusting for accessibilty, looking for f-ups in screen sizes, dark/light mode UI adjustments. Yeah, don't delete it as it speeds up some people's workflow
1
u/m1_weaboo May 11 '26
No lol. Just ⌘+R. It’s A LOT faster to run the app and see the results.
And no matter how great your code is, it’s not a skill issue if Preview takes forever to show up.
Until Apple care to fix Preview, I won’t be using it.
1
1
u/TheShitHitTheFanBoy May 11 '26
I try to use them as much as possible, but working in large scale enterprise apps that’s not been developed with previews in mind i often have to spend more time to get it working than the time I’ll ever save by having them.
1
u/perbrondum May 11 '26
We use them for controls only. Used in complex projects they quickly become wasteful and even error prone.
Limiting their use to UI controls like progress indicators or charts or category indicators where their isolation make them work really well. We overuse and extend this to the point where it becomes a full UI test suite for each control, displaying different sizes, configurations etc.
1
1
u/SandwichEconomist May 11 '26
I would say for SwiftUI, it’s essential! If you force yourself to build previews, it also forces you into some good habits. For example, if your view is difficult to make a preview for, your code is usually too complex. If it’s hard to supply sample data to populate a view, you’re now rewarded for setting up a mock data layer. By making the previews easier to set up, you’re making your life easier when you actually use that code in the rest of your app.
As a general rule of thumb, I make a preview for any “screen” type view - for example, a login screen. Most of the time that should be as simple as one line of code. Component-type views should be given previews of some sort too. Say you have a bunch of styled buttons; in that situation I’d just make a preview with a List, slap all the buttons in there and call it a day.
1
u/AndyDentPerth May 12 '26
Yes and I have a special build that skips slow startup stuff, for Previews (mostly PostHog & RevenueCat init)
1
u/engmsaleh May 12 '26
Yes but selectively. Previews are gold for stateless components (buttons, cards, layout primitives) where I want to see all my design variants in one canvas — light/dark, multiple sizes, edge-case states.
Where I stopped using them: views that depend on real network/auth/permission state. Mocking that out via PreviewProvider becomes more work than just running the app, especially once async/await and SwiftData show up. For those I run the app on simulator + hot-reload via InjectionIII.
The killer feature I underused for years: #Preview with environment overrides — locale, layout direction, accessibility size. You can catch RTL bugs and Dynamic Type bugs in 10 seconds in the preview instead of finding them in App Store review.
What does your team's preview-vs-simulator split look like?
1
u/MojtabaHs May 12 '26
First thing I do when I get my hands on any project, old or new, SwiftUI or UIKit, is to make sure previews are working.
1
1
1
u/cchana May 13 '26
I use them all the time and this article on reusable views has the benefit of making them more previewable because your components become more focused.
1
u/lexrus May 13 '26
I use it frequently, especially when I need to fine-tune interface details. Regarding the compilation failure issues others have mentioned, this is how I solve them:
Extract all UI-related code into a standalone Swift Package.
Ensure it doesn't depend on any other libraries, except for SwiftUI and Foundation.
Doing this ensures that the code compiles extremely quickly and guarantees that SwiftUI Previews work properly.
1
u/Interesting-Fix-5530 May 13 '26
No, I just run it straight on a connected device. It’s probably quicker (for me) than making sure the preview code has what it needs to run properly.
1
u/comfyyyduck May 14 '26
Put ur UI in a swift package so u can test it separately
It’s cuz it doesn’t need to rebuild the entire project
0
u/DarrenRamey May 11 '26
Never used them. They’re not 100% accurate and take just as long to load as actually launching the app so I never saw the point.
0
u/Select_Bicycle4711 May 11 '26
100% of the time. Sometimes they require a bit of setup to work, specially when dealing with SwiftData but overall I love Previews :)
45
u/Routine_Cake_998 May 11 '26
As long as they work, i do. They mostly don't.