【问题标题】:SwiftUI macOS List Row removing paddingSwiftUI macOS 列表行删除填充
【发布时间】:2020-06-28 15:15:54
【问题描述】:

我正在尝试在 MacOS 的 SwiftUI 中的 ListView 中显示一行。 但是,我的问题是该行包含围绕它的填充。我想把它延伸到我的ListView 的边界。

Apple 在他们的示例中使用.listRowInsets(EdgeInsets())。但是,这仅在 iOS 上显示。对我来说,它不适用于 macOS。

我的行有一个红色边框来显示问题。我希望它一直延伸到列表行的边界,所以填充整个蓝色行。 在 macOS 中可以吗?

提前致谢。

【问题讨论】:

  • 使用自定义 XIB/NIB 文件。管理它要容易得多。然后删除分隔线。
  • 向我们展示您的“ListView”的简化代码

标签: swift macos swiftui


【解决方案1】:

目前我找到了唯一的解决方法(.listRowInsets 确实应该完成这项工作,因此值得向 Apple 提交反馈):

struct TestListRow: View {
    var body: some View {
        List {
            ForEach (0..<3) { i in
                HStack {
                    Text("Test row \(i)").font(.largeTitle)
                    Spacer()
                }
            }
            .listRowBackground(Color.green)
            .border(Color.red)
            .padding(.horizontal, -8)   // << workaround !!
        }.environment(\.defaultMinListRowHeight, 40)
        .border(Color.yellow)
    }
}

【讨论】:

  • 工作得很好.. 在哪里报告这些错误? @Asperi
  • 这会删除间距,但会完全弄乱正文。覆盖 defaultMinListRowHeight 似乎对我来说效果不佳。
  • 嗯 defaultMinListRowHeight 和插图似乎解决了我的问题
  • 它对我不起作用。你遇到过更多的问题吗?我正在使用 Xcode beta imgur.com/gallery/lJqY69f
【解决方案2】:

我在行尾添加了这段代码,填充被缩小了!!!

    Spacer()
    Text(" ")
    .padding()

示例代码

import SwiftUI


struct testViews1: View {
    var body: some View {
        List {
            ForEach (0..<3) { i in
                HStack {
                    Text("row \(i)")
                    Spacer()
                }
                .background(Color.red)
                .frame(height: 30)  
            }
        }
    }
}


struct testViews2: View {
    var body: some View {
        List {
            ForEach (0..<3) { i in
                HStack {
                    Text("row \(i)")
                    Spacer()
                    Text(" ")
                        .padding()   
                }
                .background(Color.red)
                .frame(height: 30)
                
            }
        }
    }
}

struct testViews_Previews: PreviewProvider {
    static var previews: some View {
        testViews1()
            .frame(height: 150)
        testViews2()
            .frame(height: 150)

    }
}

输出

它适用于 SwiftUI 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2021-09-06
    • 2022-01-25
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多