How to detect whether targetEnvironment is iPadOS in SwiftUI?

I use this in my code: private var idiom : UIUserInterfaceIdiom { UIDevice.current.userInterfaceIdiom } private var isPortrait : Bool { UIDevice.current.orientation.isPortrait } Then you can do this: var body: some View { NavigationView { masterView() if isPortrait { portraitDetailView() } else { landscapeDetailView() } } } private func portraitDetailView() -> some View { if idiom … Read more

How can I wait for an async function from synchronous function in Swift 5.5?

You could maybe argue that asynchronous code doesn’t belong in setUp(), but it seems to me that to do so would be to conflate synchronicity with sequential…icity? The point of setUp() is to run before anything else begins running, but that doesn’t mean it has to be written synchronously, only that everything else needs to … Read more

How can I use async/await with SwiftUI in Swift 5.5?

I’m the author of the article you referenced. As discussed in Discover concurrency in SwiftUI, views can make use of the new .task { } and .refreshable { } modifiers to fetch data asynchronously. So you now have the following options to call your async code: func someSyncMethod() { doSomeSyncWork() Task { await methodThatIsAsync() } … Read more

Use reference to captured variable in concurrently-executing code

In short, something has to be modified from the main thread and only Sendable types can be passed from one actor to another. Let’s dig in the details. something has to be modified from the main thread. This is because @Published properties in an ObservableObject have to be modified from the main thread. The documentation … Read more

How to have text in shapes in SwiftUI?

Here is what I consider to be a more comprehensive answer. This will work as of Xcode 11.5: Text(question) .fixedSize(horizontal: false, vertical: true) .multilineTextAlignment(.center) .padding() .frame(width: 300, height: 200) .background(Rectangle().fill(Color.white).shadow(radius: 3)) Notes: fixedSize() will let the text wrap (since .lineLimit(nil) no longer is working). You can omit it if you simply want one line of … Read more

SwiftUI dynamic List with Sections does not Layout correctly

Giving List a set of items seems to make it incorrectly treat Section as a single view. You should probably file a radar for this, but in the meantime, this will give you the behavior you’re looking for: struct ListView : View { let mygroups = [ TestData(title: “Numbers”, items: [“1″,”2″,”3”]), TestData(title: “Letters”, items: [“A”,”B”,”C”]), … Read more

tech