XRay
XRay is view debugging tool for iOS. Currently, XRay can show all of the view hierarchies in UIKit. For SwiftUI, I'm working on it.
XRay helps you debugging view without using XCode's Debug View Hierarchy
Demo
XRay.mp4
How to Use
Use XRay in Debug mode only. I don't recommend using it with Swizzling function.
- Add XRay in your project
- Set ScreenShot Notification (AppDelegate.swift)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
#if DEBUG
NotificationCenter.default.addObserver(
self, selector: #selector(screenshotTaken),
name: UIApplication.userDidTakeScreenshotNotification, object: nil
)
#endif
return true
}
#if DEBUG
@objc func screenshotTaken() {
guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let keyWindow = scene.keyWindow,
let topViewController = keyWindow.topViewController() else {
return
}
let xray = XRay(rootViewController: topViewController)
xray.captureXray(classNameOption: .all)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) {
xray.removeXray()
}
}
#endif
ClassNameOption
func captureXray(classNameOption: ClassNameOption)
Labels
- Red Label(Center):
ViewController
- Blue Label(trailing bottom):
Custom View
- Black Label(leading top):
View
ClassNameOption.all
This option shows all of the view hierarchies.
ClassNameOption.customClass
This option shows custom view only
Reference
- https://stackoverflow.com/questions/6131205/how-to-find-topmost-view-controller-on-ios
- https://en.wikipedia.org/wiki/Complementary_colors
- https://gist.github.com/klein-artur/025a0fa4f167a648d9ea
- https://stackoverflow.com/questions/2746478/how-can-i-loop-through-all-subviews-of-a-uiview-and-their-subviews-and-their-su