SwiftUI duplizierte Rahmen in VStack / HStack

Aug 18 2020

Ich habe viele Kacheln Ansicht in einem HStack und VStack. Jede Kachel sollte einen Rand haben. Das Problem, mit dem ich konfrontiert bin, ist, dass ich keinen Abstand in meinem Stapel haben möchte. Dies führt jedoch zu einem doppelten Rand, da die Ansicht nebeneinander liegt.

Hier ist mein Beispiel:

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)
    }
}

Der Rand unten hat eine Breite von 1,0. Überall dort, wo es einen Nachbarn gibt, wird die Grenze jedoch 2,0 breit sein. Gibt es dafür eine Lösung? Ich müsste den Rand nur an speziellen Kanten festlegen, damit ich keine Duplikate bekomme. Aber das ist nicht möglich, meine Standardeinstellung in SwiftUI.

Antworten

1 Asperi Aug 18 2020 at 15:32

Lassen Sie uns unsere Gedanken auf den Kopf stellen ... und so etwas wie das Folgende verwenden

Getestet mit Xcode 11.4 / macOS 10.15.6

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))
    }
}
1 lvollmer Aug 19 2020 at 14:34

Sie können den Abstand verwenden: -1,0 (oder was auch immer Ihre Randbreite ist) :)

struct TileMain: View {
  var body: some View {
    VStack(spacing: -1.0)
    {
      HStack(spacing: -1.0)
      {
        Tile()
          .border(Color.red, width: 1.0)
        Tile()
          .border(Color.red, width: 1.0)
        Tile()
          .border(Color.red, width: 1.0)
      }
      HStack(spacing: -1.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)
    }
  }

}