Created
December 29, 2024 10:58
-
-
Save saschagordner/e81984f364fa9350aca360eeede807e9 to your computer and use it in GitHub Desktop.
SwiftUI Debug Frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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