NavigationView 中的 SwiftUI 列表分隔符问题
我不确定这是否是众所周知的问题,但这很奇怪。可以使用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.
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
navigationBarItems
将来将被弃用,因此您应该使用toolbar
代替。如果您想继续使用
navigationBarItems
,有一个更好的解决方法:其他ListStyle
不要在每一行上应用
navigationBarTitle
来解决问题,这很糟糕navigationBarItems
will be deprecated in the future so you should usetoolbar
insteadIf you want to keep using
navigationBarItems
, there is a better work around by using otherListStyle
do not apply
navigationBarTitle
on every row for a work around, it's bad您需要将ANY(无论您想要什么)样式应用于列表,然后一切都会好的。
You need to apply ANY (whatever you want) style to the List and then everything will be ok.