Fix: Separator Line Not Showing in SwiftUI Using EmptyView

Fix: Separator Line Not Showing in SwiftUI Using EmptyView

Written by

in

Quick answer: EmptyView has zero size by design, so applying frame and background to it produces no visible output. Use SwiftUI’s built-in Divider() view instead, which renders a horizontal or vertical rule automatically based on its layout context.

Why this happens

EmptyView is a view type that renders nothing and occupies no space. When you apply .frame(width: 200, height: 2) and .background(Color.black) to it, the frame modifier proposes a size, but EmptyView ignores it because it has no intrinsic content. The background is drawn behind the view’s content — since there is no content, there is nothing to draw behind. The same applies to .border: it draws a border around the view’s frame, but an empty view with no layout presence produces no visible border.

SwiftUI’s layout system works by having views report their ideal size. EmptyView reports a size of zero in both dimensions. Modifiers like frame and background wrap the view, but they cannot force a view to render pixels it fundamentally does not have. This is why the separator never appears.

How to fix it

1. Use the built-in Divider view (recommended)

SwiftUI provides Divider specifically for this purpose. It renders a thin rule that adapts to its container: horizontal in a VStack, vertical in an HStack.

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World")
            Divider()
            Text("Hello Another World")
        }
    }
}

Divider uses the current foreground color by default. To customize the color, apply .foregroundColor or .tint:

Divider()
    .foregroundColor(.gray)

You can also adjust thickness with .frame(height:) for horizontal dividers or .frame(width:) for vertical ones:

Divider()
    .frame(height: 2)
    .background(Color.primary)

2. Create a custom labeled divider

When you need a divider with text in the middle (e.g., “or” between sign-in options), compose Divider views in an HStack with a Text label. The key is wrapping each Divider in a VStack so it lays out horizontally inside the HStack.

struct LabelledDivider: View {
    let label: String
    let horizontalPadding: CGFloat
    let color: Color

    init(label: String,
         horizontalPadding: CGFloat = 20,
         color: Color = .gray) {
        self.label = label
        self.horizontalPadding = horizontalPadding
        self.color = color
    }

    var body: some View {
        HStack {
            line
            Text(label).foregroundColor(color)
            line
        }
    }

    private var line: some View {
        VStack { Divider().background(color) }
            .padding(horizontalPadding)
    }
}

Usage:

VStack {
    NavigationLink(destination: SignInView()) {
        Text("Sign In")
    }
    LabelledDivider(label: "or")
    NavigationLink(destination: SignUpView()) {
        Text("Sign Up")
    }
}
.padding(20)

3. Build a fully customizable line view

For complete control over thickness, color, dash pattern, or gradient, draw a Rectangle or use Path with .stroke. This avoids any layout quirks of Divider.

struct CustomSeparator: View {
    let color: Color
    let height: CGFloat
    let dashPattern: [CGFloat]?

    init(color: Color = .gray,
         height: CGFloat = 1,
         dashPattern: [CGFloat]? = nil) {
        self.color = color
        self.height = height
        self.dashPattern = dashPattern
    }

    var body: some View {
        Rectangle()
            .fill(color)
            .frame(height: height)
            // For dashed lines, use stroke instead:
            // .stroke(style: StrokeStyle(lineWidth: height, dash: dashPattern ?? []))
    }
}

Use it anywhere a view is expected:

VStack(spacing: 16) {
    Text("Section 1")
    CustomSeparator(color: .blue, height: 2)
    Text("Section 2")
    CustomSeparator(color: .red, height: 1, dashPattern: [5, 3])
    Text("Section 3")
}

How to prevent it

  • Never use EmptyView for visual elements — it exists for type-erasure and conditional view composition, not rendering.
  • Prefer Divider for standard separators — it respects accessibility, Dynamic Type, and system appearance automatically.
  • Wrap Divider in VStack when placing it inside HStack — otherwise it rotates to vertical.
  • Test in both light and dark mode — Divider adapts; custom colors may need .colorScheme handling.
  • Use .frame on Divider only for thickness — avoid constraining width/height in ways that fight the layout system.

FAQ

Why does Divider appear vertical in my HStack?

Divider infers its orientation from the stack axis. In an HStack it becomes vertical; in a VStack it becomes horizontal. Wrap it in a VStack inside the HStack to force horizontal layout.

Can I change the thickness of a Divider?

Yes. Apply .frame(height: 2) for a horizontal divider or .frame(width: 2) for a vertical one. Combine with .background(Color) if the foreground color alone doesn’t produce the desired thickness.

Is there a performance difference between Divider and a Rectangle?

Negligible for typical use. Divider is optimized for the common case and handles system theming. Use Rectangle or Path only when you need dashed lines, gradients, or precise stroke control that Divider doesn’t expose.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.