Multiline Text does not work in a NavigationLink i

2020-07-22 18:37发布

Multiline Text in a NavigationLink inside of a List does not seem to work.

Here is the code:

struct ContentView : View {
    var body: some View {
        List(1...5) { _ in
            NavigationLink(destination: EmptyView()) {
                Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
                    .lineLimit(nil)
            }
        }
    }
}

enter image description here

Removing the NavigationLink, the Text behaves as expected.

Is there a way to fix this, or is this a bug?

2条回答
聊天终结者
2楼-- · 2020-07-22 19:21

In the current XCode 13.1 Beta I still have an issue with this when the Text view is inside a container view. I could solve this giving the row (the container) a minimum height:

Text("blabla").lineLimit(2).frame(minHeight: 50)
查看更多
一纸荒年 Trace。
3楼-- · 2020-07-22 19:30

UPDATE

It seems Beta 5 has solved this bug!


Workaround for Beta 4 and previous versions:

It seems NavigationLink is "broken". But you can use DynamicNavigationDestinationLink instead. I know it's too verbose, but if you need a way out, here you have it. At least until NavigationLink works better.

struct ContentView: View {

    var body: some View {
        NavigationView {
            TopView().navigationBarTitle(Text("Top View"))
        }
    }
}

struct TopView: View {

    let detailView = DynamicNavigationDestinationLink(id: \String.self) { data in
        DetailView(passedData: data)
    }

    var body: some View {
        List(1...5) { i in
            Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
                .lineLimit(nil)
                .tapAction { self.detailView.presentedData?.value = "Detail for Row #\(i)" }
        }
    }
}

struct DetailView: View {
    let passedData: String

    var body: some View {
        Text(passedData)
    }
}
查看更多
登录 后发表回答