Random Lessons from the SwiftUI Digital Lounge

One of this year’s WWDC highlights was the introduction of the Digital Lounges. Unfortunately, it was as good as it was ephemeral. In an instant… puff! it was gone!

Many weren’t able to attend due to other commitments, lack of time, failure to register, etc. I personally failed to follow them as closely as I would have like, due to an inconvenient time zone (I’ll plan better next year). Still, before the channel was taken down, I managed to take my notes. Fortunately, when the moderators were asked, they said the information is not considered private and can be shared with others.

In that spirit, I have categorized, curated, and in some cases commented the SwiftUI Digital Lounge questions. For better and faster browsing, I simplified many of the questions to one (or two) lines. Still, the original text can be found inside. I have also removed all names, just in case someone is uncomfortable with it.

In some of the questions, I added my own comments, by either expanding on the topic, or appending an example of what is discussed. These questions are marked with 💬.

When a question is related to a new feature introduced in WWDC ’21, I flagged it with a ♦️ symbol.

I also put a ⭐️ mark, on those questions that I found particularly interesting. Some of the reasons why a question gets the star are:

  1. The answer provides a brand new piece of information.
  2. The answer from Apple confirms something we suspected for a long time, but wasn’t documented anywhere.
  3. The answer from Apple confirms a pattern we’ve adopted in the community, but not seen used by Apple until now.
  4. The question discusses a topic rarely treated and deserves some attention.
  5. The answer provides some insight into the inner workings of the framework.
  6. Or just any other question that made me stop to think 🤔

Introduction over… let’s start questioning…

Animations

Is there a way to animate a View moving from one place in hierarchy to another? Like when a View changes parents? 💬

Is there a way to animate text font size changes? ♦️ 💬

Is it safe to employ multiple TimelineView with the animation schedule? ♦️ ⭐️

AppKit/UIKit

How can we get access to the underlying AppKit/UIKit that SwiftUI is built on? 💬

Does SwiftUI have an equivalente to UIView’s drawHierarchy to render the view to an image?

How can I control the ideal size of a UIViewRepresentable View?

Is it ok to use AnyView with UIHostingController? ⭐️

Why does a UIViewRepresentable update once after makeUIView and once before dismantleUIView?

when creating a UIViewRepresentable, it is dangerous for the Coordinator to hold an instance of the UIView passed in updateUIView() or should it be strictly treated as ephemeral? ⭐️

Is there any way to convert from the old AppDelegate/SceneDelegate lifecycle to the new “SwiftUI 2” lifecycle?

Backward Compatibility

Can you talk about ways to have existing SwiftUI code inherit the newer features so I could support both iOS 14 and 15 with one codebase? 💬

Is the @ViewBuilder to remove AnyView (“Demystify SwiftUI” talk) only available with iOS 15? ♦️

Coding Strategies

Do situations exist when the developers really need to use AnyView and there are totally no alternatives using Swift constructs? ⭐️

Which view body is computed first: child or parent? ⭐️

How can we pass a view to a ViewModifier, without AnyView? ⭐️ 💬

Group or ViewBuilder?

Can we use .id() to keep view equality? ⭐️

Is there any situation in which Hashable may be preferred to Identifiable?

How can we use conditional modifiers with styles? ♦️

Is there a good way to apply a modifier to a SwiftUI view conditionally?

When using the new SwiftUI Table View, can you Group to have more than 10 TableColumns? ♦️ ⭐️ 💬

How to avoid AnyView with Core Data and UIHostingController ⭐️ 💬

If SE-309 comes to effect and applies to View, will there be a difference between a View existential and AnyView in terms of view identity? ♦️ ⭐️

Are there any places where you think: yep, this is the place to use onAppear rather than task? ♦️ ⭐️

Concurrency

Is the refreshable property the only SwiftUI property that supports async code? ♦️

Is it possible to back-deploy concurrency? ♦️ ⭐️

Data Management

@StateObject or ObservedObject?

Any way to control when the @SceneStorage gets saved?

Should @State and @StateObject be usually defined as private?

Is there any difference between the persistence of @State vs @StateObject vs @ObservedObject when identity changes?

What are the downsides of having the whole app state in a single StateObject set in the root view? ⭐️

Is there any difference between State/Binding vs EnvironmentObject?

Performance wise, ObservableObject or EnvironmentObject?

Is is ok to use child CoreData context as @StateObject?

Why the error “view environmentObject may be missing as an ancestor of this view”? ⭐️

May I call the StateObject initializer directly? ⭐️

Can custom property wrappers embed existing ones (e.g., State, ObservedObject, etc)? ⭐️

Can a @State variable be mutated from outside the main thread?

If an ObservedObject is passed into a view, will it get invalidated if it does not use the value? ⭐️ 💬

Why do I get nil EnvironmentObjects on sheets? ⭐️

I need to support iOS13, but I need StateObject. What should I do?

EnvironmentObject or ObservedObject: Which is better?

Debugging

How can I debug AttributeGraph crashes? ⭐️

Is there a way to profile SwiftUI code? ⭐️

Drawing

Is there an efficient way to set the values of individual pixels in a view in SwiftUI? ♦️

Is there a way to disable pixel rounding in SwiftUI?

How far can we go (performance wise), with the new Canvas view? ♦️ ⭐️

How does Canvas behave with TimelineView? ♦️ ⭐️

Focus

Is it possible to set focus programmatically this year? ♦️

Why doesn’t FocusState support an initial value? ♦️ ⭐️

Import/Export

Does the modifier of importsItemProviders work with other commands, or other stuff to import things in the app? ♦️

Can importsFromDevices (i.e., continuity camera) work as a source for AsyncImage? ♦️

Layout

What is the layout and sizing behavior of Canvas? ♦️

Is it ok to put GeometryReader inside a clear overlay to avoid impacting layout? ⭐️ 💬

How can I get the size of another view? ⭐️ 💬

The Dependency Graph — can it have loops, or is it acyclic?

How can we maintain view equality when switching its container from HStack to VStack? ⭐️ 💬

Is there enough public API to create our own containers with a fully custom layout? ⭐️

frame() or Spacer()? ⭐️ 💬

What is error: Bound preference SizePreferenceKey tried to update multiple times per frame?

List Views

How can I change a List background color?

How can we make List rows editable? ⭐️ 💬

How can I prevent a button activation when its row is selected?

Which one I choose? List(items) { … } or List { ForEach(items) { … } }

How can we sort a List?

How can we bypass system styling, for example, with List sections headers?

NavigationView

Performance

Are NavigationLinks “lazy” in iOS 15? ♦️ ⭐️

Does AsyncImage support caching? ♦️

Does using long strings as ID present a problem for performance? ⭐️

Are there any combinations of parameters in .frame(maxWidth:…) more performant than others? ⭐️

Sample code in the Advanced Graphics section of Whats New in SwiftUI is too slow? Why? ♦️ ⭐

Do we incur in a performance penalty for using ViewBuilders? ⭐️

Animating changes of the data set on List views is very slow. Is this expected? ♦️ ⭐️

Is GeometryReader bad for performance? ⭐️

Presenting Views

What is the proper way to dismiss a .fullscreenCover?

How to avoid error “Attempt to present <X> on <Y> which is already presenting <Z>”?

What’s the recommended practice for custom sheet presentations? ⭐️ 💬

Refreshable

Can the new .refreshable API be used with custom refresh controls/spinners? ♦️ ⭐️ 💬

Is there a way to make refreshable work with LazyVGrid? ♦️ 💬

Does refreshable() work with ScrollView? ♦️

Searching

Using @Environment(.isSearching), how would multiple navigation bar in an app work? ♦️

Is .searchable for local data only, or can it be used to query services? ♦️

How would one go about adding the .searchable modifier to a PDFView? ♦️

SF Symbols

Can we use the new buttons with non-SF single-color graphics? ♦️

Does using more SF Symbols have an impact on total app size? Or they all are stored in the OS?

Is there a limit to where SFSymbols change size?

Text

Are paragraph styles supported by AttributedStrings in Text? ♦️

Can the new AtrributedString be used to provide accessibility notations in SwiftUI? ♦️

Markdown text in a variable won’t work. Is it a bug? ♦️ 💬

WatchKit

When using the WatchKit way to get text input, do we still get the text input improvements from SwiftUI? (e.g. remembering the user choice of scribble vs. voice input)

Window Management

What is the recommended way to open a View in a new window in SwiftUI for macOS? (1)

What is the recommended way to open a View in a new window in SwiftUI for macOS? (2) ⭐️

Is there a way for a view to know when the window it is hosted in is key or not on macOS?

What is the handlesExternalEvents modifier? ⭐️

For SwiftUI macOS document apps, is there a recommended way to save and restore window size and position?


Summary

Congratulations for reaching the end of this long post. I hope you enjoyed the SwiftUI Digital Lounge content.

Please feel free to comment below, and follow me on twitter if you would like to be notified when new articles come out. Until next time!

10 thoughts on “Random Lessons from the SwiftUI Digital Lounge”

  1. Thank you for this 🙏

    About the AnyView discussion what do you think “..okay if used in situations where the wrapped view will rarely or never change.” means ? What should rarely change, the view identity or the view state ?
    e.g. : Changing the erased view from a Rectangle to a Circle or changing from a red Rectangle to a green one.

    Reply
  2. Hello, Thanks for this compilation!

    I’m the one who asked the question “frame() or Spacer()?”.
    There is a little misunderstanding about .frame(maxWidth:) I mean. In your example, what I said is something like:

    
    HStack {
        Circle()
            .frame(width: 50, height: 50)
            .frame(maxWidth: .infinity, alignment: .trailing)
        Circle()
            .frame(width: 50, height: 50)
            .frame(maxWidth: .infinity, alignment: .trailing)
    }
    

    But It doesn’t affect the final conclusion you are saying. So, anyway 😉

    BTW, I really learned A LOT from SwiftUI-Lab! Especially the three articles about PreferenceKey.
    Thank you for this incredible blog!

    Reply
    • Hi Lynn! Thanks for the question. When I was going through it, I had to stop to think… Since there wasn’t an example, I though it was a good idea to find a case where one would expect both to behave identically, but didn’t. I realized it wasn’t probably what the poster (you ;-), had in mind… but better share than not 😉

      Reply
  3. Thank you for this site and the articles.
    Especially this page on the content of the Digital Lounges.
    I have personally been occupied by many things other, and is only beginning to catch up with WWDC21 and the new features of IOS15.
    This is invaluable.
    Thank you
    Regards
    Lars

    Reply

Leave a Comment

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close