Skip to content

Instantly share code, notes, and snippets.

@mikehouse
Last active October 3, 2023 08:54
Show Gist options
  • Save mikehouse/cea4bc99b827debcea0e26deecbee8ef to your computer and use it in GitHub Desktop.
Save mikehouse/cea4bc99b827debcea0e26deecbee8ef to your computer and use it in GitHub Desktop.
SwiftUI View Debug Tracking
extension View {
public func debugView<T: View>(_ type: T.Type, file: String = #filePath, line: UInt = #line) -> some View {
#if DEBUG
return background(DebugView(type: type, file: file, line: line))
#else
return self
#endif
}
}
private struct DebugView<T>: UIViewRepresentable {
let type: T.Type
let file: String
let line: UInt
func makeUIView(context: Context) -> UIView {
return UIView()
}
func updateUIView(_ view: UIView, context: Context) {
view.accessibilityIdentifier = "\(type) \(URL(fileURLWithPath: file).lastPathComponent):\(line)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment