Post

Replies

Boosts

Views

Activity

SwiftUI matchedGeometryEffect not working with NavigationView
I'm trying to used matchedGeometryEffect on a pair of views. It works well until you navigate to a child view and then back, in which case the matchedGeometryEffect seems broken briefly (the red rectangle is instantly visible when I try expanding my view) Is there something I'm missing? struct ContentView: View { @Namespace private var namespace @State private var expanded = false var body: some View { NavigationView { VStack { NavigationLink("Click Me") { Text("Hello, world") } Group { if expanded { Rectangle() .foregroundColor(.red) .matchedGeometryEffect(id: "Rect", in: namespace) .frame(width: 300, height: 300) } else { Rectangle() .foregroundColor(.blue) .matchedGeometryEffect(id: "Rect", in: namespace) .frame(width: 50, height: 50) } } .onTapGesture { withAnimation(.linear(duration: 2.0)) { expanded.toggle() } } } } } }
1
0
745
Nov ’22
Navigation bar briefly flickering as large title before changing to inline
I'm using a UITabController in my SwiftUI app and it works well, except for one issue. When I have a NavigationView that uses a large title, it changes to inline as I scroll, as expected. So far so good. When I switch tabs, then switch back again, it should remember the scroll position and therefore be inline. Instead, it very briefly flickers as a large title before moving to inline. This only seems to happen on device. Screen Recording - https://developer.apple.com/forums/content/attachment/8e27f663-5350-4751-8d70-2b41fc12b40f Is any able to help me with a workaround? struct ContentView: View {     var body: some View {         CustomTabView([             Tab(view: FirstView(),                 barItem: UITabBarItem(title: "First", image: UIImage(systemName: "1.circle"), selectedImage: nil)),             Tab(view: SecondView(),                 barItem: UITabBarItem(title: "Second", image: UIImage(systemName: "2.circle"), selectedImage: nil))         ])     } } struct FirstView: View {     var body: some View {         NavigationView {             List {                 ForEach( 0...40, id: \.self ) {                     Text("\($0)")                         .padding()                 }             }             .navigationTitle("First View")         }     } } struct SecondView: View {     var body: some View {         NavigationView {             List {                 ForEach( 41...80, id: \.self ) {                     Text("\($0)")                         .padding()                 }             }             .navigationTitle("Second View")         }     } } struct TabBarController: UIViewControllerRepresentable {     var controllers: [UIViewController]     func makeUIViewController(context: Context) - UITabBarController {         let tabBarController = UITabBarController()         tabBarController.viewControllers = controllers         tabBarController.delegate = context.coordinator         return tabBarController     }     func updateUIViewController(_ uiViewController: UITabBarController, context: Context) {}          func makeCoordinator() - Coordinator {         Coordinator()     }          class Coordinator: NSObject, UITabBarControllerDelegate {         func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) - Bool {             guard let fromView = tabBarController.selectedViewController?.view, let toView = viewController.view else {                 return false             }             if fromView != toView {                 fromView.superview!.addSubview(toView)             }             return true         }     } } struct CustomTabView: View {     var viewControllers: [UIHostingControllerAnyView]     init(_ tabs: [Tab]) {         self.viewControllers = tabs.map {             let host = UIHostingController(rootView: $0.view)             host.tabBarItem = $0.barItem             return host         }     }     var body: some View {         TabBarController(controllers: viewControllers)             .edgesIgnoringSafeArea(.all)     } } struct Tab {     var view: AnyView     var barItem: UITabBarItem     initV: View(view: V, barItem: UITabBarItem) {         self.view = AnyView(view)         self.barItem = barItem     } }
0
0
2.1k
Mar ’21