Skip to content

Instantly share code, notes, and snippets.

@saschagordner
Created December 29, 2024 10:58
Show Gist options
  • Save saschagordner/e81984f364fa9350aca360eeede807e9 to your computer and use it in GitHub Desktop.
Save saschagordner/e81984f364fa9350aca360eeede807e9 to your computer and use it in GitHub Desktop.
SwiftUI Debug Frame
#if DEBUG
struct DebugFrameModifier: ViewModifier {
let color = Color.red
let lineWidth = 0.5
let spacing: CGFloat = 16
func body(content: Content) -> some View {
content
.border(color)
.overlay(
GeometryReader { geometry in
ZStack {
// Vertical lines
ForEach(0...Int(geometry.size.width / spacing), id: \.self) { i in
Path { path in
let x = CGFloat(i) * spacing
path.move(to: CGPoint(x: x, y: 0))
path.addLine(to: CGPoint(x: x, y: geometry.size.height))
}
.stroke(color.opacity(0.5), lineWidth: lineWidth)
}
// Horizontal lines
ForEach(0...Int(geometry.size.height / spacing), id: \.self) { i in
Path { path in
let y = CGFloat(i) * spacing
path.move(to: CGPoint(x: 0, y: y))
path.addLine(to: CGPoint(x: geometry.size.width, y: y))
}
.stroke(color.opacity(0.5), lineWidth: lineWidth)
}
}
}
)
}
}
extension View {
func debugFrame() -> some View {
modifier(DebugFrameModifier())
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment