SwiftUI - NavigationLink Bug 内部的 NavigationLink

SwiftUI - NavigationLink inside of NavigationLink Bug

我正在尝试在我的 NavigationLink 中创建一个 NavigationLink 列表作为 DetailView。 可悲的是我越来越奇怪 UI 错误。这是代码:

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"))
            }
        }
    }
}

这是错误消息:

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:
<_TtGC7SwiftUIP10c5cea4419ListCoreCoordinatorGVS_20SystemListDataSourceOs5Never_GOS_19SelectionManagerBoxS2___:
0x7fbdc25a4470>>, new observer <UINavigationController:
0x7fbdc3875a00>, removing old observer <UINavigationController: 0x7fbdc3840c00>

这是 UI 错误:

您不需要 DetailView 中的另一个 NavigationView - 它已经进入 ContentViewNavigationView。你可以阅读 this tutorial,那里回答了这个问题。你的代码应该是这样的:

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")
    }
}

你会看到: