NavigationView 中的 SwiftUI 列表分隔符问题

发布于 2025-01-12 13:02:15 字数 2909 浏览 0 评论 0原文

我不确定这是否是众所周知的问题,但这很奇怪。可以使用Apple的示例代码重现该问题 navigationBarItems(leading:trailing :) 正如您所看到的,列表分隔符有额外的前导空格,看起来像是由于某种原因缩进的。

输入图片这里的描述

这是实际代码:

import Foundation
import SwiftUI
import UIKit
import PlaygroundSupport

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Text("Chocolate")
                Text("Vanilla")
                Text("Strawberry")
            }
            .navigationBarTitle(Text("Today‘s Flavors"))
            .navigationBarItems(leading:
                HStack {
                    Button("Hours") {
                        print("Hours tapped!")
                    }
                }, trailing:
                HStack {
                    Button("Favorites") {
                        print("Favorites tapped!")
                    }

                    Button("Specials") {
                        print("Specials tapped!")
                    }
                }
            )
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

我用 Playground 测试了上面的代码,iPhone 13/15.3.1 它们是相同的。 我搞乱了代码,发现将 .navigationBarTitle()、.navigationBarItems() 应用于 List 会导致问题。它们必须适用于每个列表项。不过很奇怪。这意味着几乎所有用 NavigationView 包装的 List 示例代码都是错误的。这是我找到的修复方法。

输入图片这里的描述

import Foundation
import SwiftUI
import UIKit
import PlaygroundSupport

struct ContentView: View {
    let contents = ["Chocolate", "Vanilla", "Strawberry"]

    var body: some View {
        NavigationView {
            List {
                ForEach (contents, id: \.self) { content in
                    Text(content)
                        .navigationBarTitle(Text("Today‘s Flavors"))
                        .navigationBarItems(leading:
                            HStack {
                                Button("Hours") {
                                    print("Hours tapped!")
                                }
                            }, trailing:
                            HStack {
                                Button("Favorites") {
                                    print("Favorites tapped!")
                                }

                                Button("Specials") {
                                    print("Specials tapped!")
                                }
                            }
                        )
                }
            }
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

虽然我不确定是否可以称这是一个错误,但文档或实现肯定是错误的。有人能解释一下吗?

I'm not sure if this is well-known-issue or not but it is very odd. The problem can be reproduced with Apple's example code navigationBarItems(leading:trailing:) As you can see, list separators have extra leading space that look like they are indented for some reason.

enter image description here

Here is actual code:

import Foundation
import SwiftUI
import UIKit
import PlaygroundSupport

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Text("Chocolate")
                Text("Vanilla")
                Text("Strawberry")
            }
            .navigationBarTitle(Text("Today‘s Flavors"))
            .navigationBarItems(leading:
                HStack {
                    Button("Hours") {
                        print("Hours tapped!")
                    }
                }, trailing:
                HStack {
                    Button("Favorites") {
                        print("Favorites tapped!")
                    }

                    Button("Specials") {
                        print("Specials tapped!")
                    }
                }
            )
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

I test above code with Playground, iPhone 13/15.3.1 they are the same.
I did messing around the code and found that applying .navigationBarTitle(), .navigationBarItems() to List causes the problem. They must apply to each List item. Very odd though. This means almost all List sample code that wrapping with NavigationView are WRONG. Here is a fix I found.

enter image description here

import Foundation
import SwiftUI
import UIKit
import PlaygroundSupport

struct ContentView: View {
    let contents = ["Chocolate", "Vanilla", "Strawberry"]

    var body: some View {
        NavigationView {
            List {
                ForEach (contents, id: \.self) { content in
                    Text(content)
                        .navigationBarTitle(Text("Today‘s Flavors"))
                        .navigationBarItems(leading:
                            HStack {
                                Button("Hours") {
                                    print("Hours tapped!")
                                }
                            }, trailing:
                            HStack {
                                Button("Favorites") {
                                    print("Favorites tapped!")
                                }

                                Button("Specials") {
                                    print("Specials tapped!")
                                }
                            }
                        )
                }
            }
        }
    }
}

PlaygroundPage.current.setLiveView(ContentView())

Although I'm not sure if I can call this is a bug but definitely either document or implementation is wrong. Could anyone explain this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2025-01-19 13:02:16

navigationBarItems 将来将被弃用,因此您应该使用 toolbar 代替。

.toolbar {
    ToolbarItem(placement: .navigationBarLeading) {
        Button("Hours") { }
    }
}
.toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
        HStack {
            Button("Favorites") { }
            Button("Specials") { }
        }
    }
}

如果您想继续使用 navigationBarItems,有一个更好的解决方法:其他 ListStyle

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Text("Chocolate")
                Text("Vanilla")
            }
            .navigationBarTitle(Text("Today‘s Flavors"))
            .navigationBarItems(
                leading: Button("Hours") { }
            )
            .listStyle(PlainListStyle())
        }
    }
}

不要在每一行上应用 navigationBarTitle 来解决问题,这很糟糕

navigationBarItems will be deprecated in the future so you should use toolbar instead

.toolbar {
    ToolbarItem(placement: .navigationBarLeading) {
        Button("Hours") { }
    }
}
.toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
        HStack {
            Button("Favorites") { }
            Button("Specials") { }
        }
    }
}

If you want to keep using navigationBarItems, there is a better work around by using other ListStyle

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Text("Chocolate")
                Text("Vanilla")
            }
            .navigationBarTitle(Text("Today‘s Flavors"))
            .navigationBarItems(
                leading: Button("Hours") { }
            )
            .listStyle(PlainListStyle())
        }
    }
}

do not apply navigationBarTitle on every row for a work around, it's bad

不甘平庸 2025-01-19 13:02:16

您需要将ANY(无论您想要什么)样式应用于列表,然后一切都会好的。

You need to apply ANY (whatever you want) style to the List and then everything will be ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文