Last active
October 3, 2023 08:54
-
-
Save mikehouse/cea4bc99b827debcea0e26deecbee8ef to your computer and use it in GitHub Desktop.
SwiftUI View Debug Tracking
This file contains hidden or 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
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