Post

Replies

Boosts

Views

Activity

Reply to View as a Parameter / Variable in SwiftUI
Take a look at my solution: https://stackoverflow.com/a/63937440/6898849 struct ContainerView<Content: View>: View { &#9;&#9;let content: Content &#9;&#9;init(@ViewBuilder content: @escaping () -> Content) { &#9;&#9;&#9;&#9;self.content = content() &#9;&#9;} &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;// Do something with `content` &#9;&#9;} } You don't need AnyView (which you anyways should use as little as possible as it prevents all kinds of optimizations). My solution also allows you to use if-else and switch-case blocks inside (thanks to @ViewBuilder): struct SimpleView: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;ContainerView { &#9;&#9;&#9;&#9;&#9;&#9;Text("SimpleView Text") &#9;&#9;&#9;&#9;} &#9;&#9;} } struct IfElseView: View { &#9;&#9;var flag = true &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;ContainerView { &#9;&#9;&#9;&#9;&#9;&#9;if flag { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("True text") &#9;&#9;&#9;&#9;&#9;&#9;} else { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("False text") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} } struct SwitchCaseView: View { &#9;&#9;var condition = 1 &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;ContainerView { &#9;&#9;&#9;&#9;&#9;&#9;switch condition { &#9;&#9;&#9;&#9;&#9;&#9;case 1: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("One") &#9;&#9;&#9;&#9;&#9;&#9;case 2: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Two") &#9;&#9;&#9;&#9;&#9;&#9;default: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Default") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} }
Sep ’20
Reply to Updates to data from App Intent don't trigger view refresh in app
I found a nasty workaround, posted it here https://github.com/JuniperPhoton/Widget-Intermediate-Animation/issues/1. Long story short – updating a SwiftData object that (might have) changed will force SwiftData to pull the other updates to this object. So what I did was the following: .onChange(of: scenePhase) { _, newValue in if case .active = newValue { items.forEach { $0.title = $0.title } } }
Apr ’24