SwiftUI:NavigationLink 在最小的 10 行示例中导致不需要的填充

标签 swiftui padding swiftui-navigationlink

当在 NavigationLink 中包装 View 以创建主从流程时,NavigationLink 似乎添加了我不想要的填充(红色)。

enter image description here enter image description here

如果我注释掉 NavigationLink,一切看起来都是正确的(右图)。然而,这当然让我的观点无法点击。

如何去除在 Watch 上渲染时出现的红色填充?

这是我运行的代码:

struct ContentView: View {
    
    let testArray = [1,2,3,4,5]
    
    var body: some View {
        
        ScrollView {
            VStack {
                ForEach(testArray, id: \.self) { element in
                    
                    NavigationLink(destination: Text("")) {
                        Text("BB . . . . . . . . . . . . . . . . . . . . . . .")
                            .font(.title3)
                            .foregroundColor(.white)
                            .background(Color.blue)
                    }.background(Color.red) //has all the unwanted padding
                    
                }
            }.background(Color.black)
        }.background(Color.gray)
    }
}

我在 How to show NavigationLink as a button in SwiftUI 找到了一些很好的提示然而,使用 ZStack/.background 技巧只会留下一个可点击的 ~30px strip ,在这种情况下它不会填满整个 ZStack。

我发现填充似乎是静态的 8px。所以我现在正在做的是 .padding(.all, -8) 希望能有更好的解决方案

最佳答案

.buttonStyle(PlainButtonStyle()) 应用于您的 NavigationLink 以移除填充和背景。显然,您还会丢失按钮的默认样式,因此如果需要,您必须重新创建它。

关于SwiftUI:NavigationLink 在最小的 10 行示例中导致不需要的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69486613/

相关文章:

swiftui - 相对于另一个居中 View 定位 View

html - 删除填充底部高度 :auto

python - 在Python中查找不等于不需要的字节的前一个字节

css - 如何摆脱 mac 上 webkit 中 html5 搜索输入中的水平填充或缩进?

SwiftUI 井字游戏板

SwiftUI macOS 命令(菜单栏)和 View

ios - SwiftUI NavigationLink 在 Xcode 11.4 模拟器中仍然损坏?

swiftui - NavigationLink isActive 已弃用

swift - 如何在 SwiftUI 中制作粘性栏?

core-data - swiftui 如何从详细信息 View 获取核心数据值到编辑 View