UIViewControllerRepresentable: Navigation Title and Bar Button Items Ignored in NavigationView

Solved! Problem and Expectation SwiftUI uses a UINavigationController under the hood. So, if I push a UIViewController onto a SwiftUI NavigationView using UIViewControllerRepresentable, then I would expect the navigation item and toolbar items of that view controller to be used by said navigation controller. As I mention above, they are ignored. Root Cause It turns … Read more

Passing An ObservedObject To Nested Child Views SwiftUI (SwiftUI Data Flow)

For ObservableObject the pairing ObservedObject makes view refresh, so to solve the task in question I would recommend the following approach: Demo Code import SwiftUI import Combine class Sport: ObservableObject, Hashable, Identifiable { static func == (lhs: Sport, rhs: Sport) -> Bool { lhs.name == rhs.name && lhs.isFavorite == rhs.isFavorite && lhs.school == rhs.school } … Read more

didSet for a @Binding var in Swift

Instead of didSet you can always use onReceive (iOS 13+) or onChange (iOS 14+): import Combine import SwiftUI struct ContentView: View { @State private var counter = 1 var body: some View { ChildView(counter: $counter) Button(“Increment”) { counter += 1 } } } struct ChildView: View { @Binding var counter: Int var body: some View … Read more

Semi-transparent (blurry like VisualEffectView) of the view behind the current view

The Apple way Investigating on the view hierarchy shows that Apple is using UIKit and UIVisualEffectViewfor this reason. You can define a VisualEffectView with just 5 lines of code: struct VisualEffectView: UIViewRepresentable { var effect: UIVisualEffect? func makeUIView(context: UIViewRepresentableContext<Self>) -> UIVisualEffectView { UIVisualEffectView() } func updateUIView(_ uiView: UIVisualEffectView, context: UIViewRepresentableContext<Self>) { uiView.effect = effect } … Read more