In SwiftUI how can I have a View exceed its parents frame/boundary in a List?


This Content is from Stack Overflow. Question asked by fasoh

Consider the following code:

import SwiftUI

struct ContentView: View {
  private var listContent: [String] = ["This", "is", "a", "placeholder", "list"]
  var body: some View {
    List {
      ForEach(listContent, id: .self) { entry in
        listElement(text: entry)

struct listElement: View {
  @State var text: String
  var body: some View {
    Section() {
      HStack {
          .frame(width: 30, height: nil)

which generates this

I would like to add additional markers to the left of each List element and/or section, like this (mockup):

I have the data that describes the width of the green bars leading into the list elements on the child and parent element. Coming from web development I expect to do something like a child element having negative margin and is thus exceeding its parents container. However I could not get it to work with SwiftUI’s padding. Is there a way to do this?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?

Exit mobile version