swiftswiftuinavigationlink

SwiftUI - NavigationLink inside of NavigationLink Bug


im trying to create a NavigationLink List inside of my NavigationLink as a DetailView. Sadly im getting weird UI Bugs. Here is the Code:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink("label", destination: DetailView())
            }
        }
    }
}

struct DetailView : View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink("label", destination: Text("detail"))
            }
        }
    }
}

And here is the Error Message:

2020-01-20 00:30:04.302351+0100 Test[14029:1128128] [Assert] UIScrollView does not support multiple observers implementing
_observeScrollView:willEndDraggingWithVelocity:targetContentOffset:unclampedOriginalTarget:.
Scroll view <_TtC7SwiftUIP33_BFB370BA5F1BADDC9D83021565761A4925UpdateCoalescingTableView:
0x7fbdc3865c00; baseClass = UITableView; frame = (0 0; 375 667); clipsToBounds = YES;
autoresize = W+H; gestureRecognizers = <NSArray: 0x600002fb4f30>;
layer = <CALayer: 0x6000021592c0>; contentOffset: {0, -212}; contentSize: {375, 44.5}; adjustedContentInset: {212, 0, 0, 0}; dataSource:
<_TtGC7SwiftUIP10$10c5cea4419ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2___:
0x7fbdc25a4470>>, new observer <UINavigationController:
0x7fbdc3875a00>, removing old observer <UINavigationController: 0x7fbdc3840c00>

Here is the UI Bug:

Image UI Bug


Solution

  • You don't need another NavigationView in the DetailView - it's already into the NavigationView of the ContentView. You may read this tutorial, there this question is answered. And you code should looks like:

    struct NavigationLinks: View {
        var body: some View {
            NavigationView {
                List {
                    NavigationLink("label", destination: DetailViewWithNavLink())
                }
            }
        }
    }
    
    struct DetailViewWithNavLink : View {
        var body: some View {
            List {
                NavigationLink("label", destination: Text("detail")
                    .navigationBarTitle("Text from item in Detail View List", displayMode: .inline))
            }
        .navigationBarTitle("Detail view")
        }
    }
    

    and you'll see:

    enter image description here