SwiftUI 在 VStack/HStack 中重复边框

标签 swiftui

我在 HStack 和 VStack 中有很多图块 View 。每块瓷砖都应该有一个边框。我面临的问题是,我不想在我的堆栈中有任何间距。但是,这会导致重复的边框,因为 View 彼此相邻。
这是我的例子:

struct TileMain: View {
    
    var body: some View {

        VStack
        {
            HStack(spacing: 0.0)
            {
                Tile()
                    .border(Color.red, width: 1.0)
                Tile()
                    .border(Color.red, width: 1.0)
                Tile()
                    .border(Color.red, width: 1.0)
            }
            HStack(spacing: 0.0)
            {
                Tile()
                .border(Color.red, width: 1.0)

                Tile()
                    .border(Color.red, width: 1.0)
                
                Tile()
                    .border(Color.red, width: 1.0)
            }
            .padding(.bottom, 15)
        }
    }
}
struct Tile : View
{
    var body: some View
    {
        VStack
        {
            Spacer()
            Text("Test")
            Spacer()
        }.frame(width: 150, height: 150)
    }
}
enter image description here
底部的边框宽度为 1.0。然而,在任何有邻居的地方,边界都会是 2.0 宽。有什么解决办法吗?我只需要在特殊边缘设置边框,所以我不会得到任何重复。但这不可能是我在 SwiftUI 中的默认设置。

最佳答案

让我们颠倒一下……并使用以下内容
使用 Xcode 11.4/macOS 10.15.6 测试
demo

struct TileMain: View {

    var body: some View {

        VStack(spacing: 1)
        {
            HStack(spacing: 1)
            {
                Tile()
                Tile()
                Tile()
            }
            HStack(spacing: 1)
            {
                Tile()
                Tile()
                Tile()
            }
        }
        .padding(1)
        .background(Color.red)
    }
}
struct Tile : View
{
    var body: some View
    {
        VStack
        {
            Spacer()
            Text("Test")
            Spacer()
        }.frame(width: 150, height: 150)
        .background(Color(NSColor.windowBackgroundColor))
    }
}

关于SwiftUI 在 VStack/HStack 中重复边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63464476/

相关文章:

macos - 无法在 macOS 应用程序的 FFMPEG 进程中通过 NSOpenPanel 访问用户选择的文件

每次渲染主体时调用 SwiftUI Picker onReceive()

ios - 如何在 SwiftUI 中将自定义字体系列设置为整个应用程序的默认值

ios - SwiftUI 中带有 UIViewRepresentable 的 UITableView

swift - 在 SwiftUI 中修改 ForEach 中的结构数据

SwiftUI 将@Published View 模型对象值传递给@Binding

list - 将带标题的部分添加到具有可扩展行的 SwiftUI 列表

ios - 在构建 iOS 应用程序时,在范围错误中获取找不到类型 'UIHostingController'

swift - 如何设置窗口的最小尺寸并使其在启动时使用指定的尺寸和位置?

ios - 关于状态更改问题的 SwiftUI 刷新列表