UIDebuggingInformationOverlay(譯文)

原文:http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/

翻譯不準確的地方可以隨時聯系我做修改,轉載請注明來源


While browsing UIKit’s private headers recently, I came across a class that I hadn’t seen before - UIDebuggingInformationOverlay. A Google search didn’t turn up much info, so figured I’d write a short description of what I’ve found.

最近在瀏覽 UIKit 的私有頭部時,我遇到了一個以前沒看過的類UIDebuggingInformationOverlayGoogle搜索沒有找到太多的信息,所以我想寫一個簡短的描述,關于我發現了什么。

UIDebuggingInformationOverlay is a private UIWindow subclass created by Apple, presumably to help developers and designers debug Apple’s own iOS apps.

UIDebuggingInformationOverlay 是由 Apple 創建的一個私有的 UIWindow 子類,大概是幫助開發者和設計師調試蘋果自己的 iOS App。

When enabled, the window floats over your app’s content, like so:
啟用后,UIDebuggingInformationOverlay 會浮動在當前 App 的上層,如下所示:

uidebugginginformationoverlay_0.png

In this post, I’ll show you how to make it display. I’ll also summarize its features, at least as I understand them.

這篇文章里,我將想你展示如何使 UIDebuggingInformationOverlay 顯示出來。也會總結下他的功能。

How to show UIDebuggingInformationOverlay

如何顯示 UIDebuggingInformationOverlay

There are two steps needed to show UIDebuggingInformationOverlay in all its glory:

  1. Call [UIDebuggingInformationOverlay prepareDebuggingOverlay] - I’m not sure exactly what this method does, but the overlay will be empty if you don’t call it.
  2. Call [[UIDebuggingInformationOverlay overlay] toggleVisibility] - This shows the overlay window (assuming it’s not already visible).

顯示 UIDebuggingInformationOverlay的兩個步驟:

  1. 調用 [UIDebuggingInformationOverlay prepareDebuggingOverlay] - 我不清楚這個方法究竟是做什么的,但是如果不調用它,覆蓋圖將是空的。
  2. 調用 [[UIDebuggingInformationOverlay overlay] toggleVisibility] - 顯示覆蓋窗口(假設它不是可見的)。

Because the class and its methods are private, you’ll need to jump through a few hoops to get the code to compile. One approach is to use the global ‘NSClassFromString’ and ‘NSSelectorFromString’ functions. In Swift, this approach looks something like:

因為 UIDebuggingInformationOverlay 類 和它的方法是私有的,你需要跳過幾個環節去得到需要的代碼完成編譯。一種途徑是使用全局 NSClassFromStringNSSelectorFromString 函數。在 Swift 中,這種方法看起來像:

let overlayClass = NSClassFromString("UIDebuggingInformationOverlay") as? UIWindow.Type
_ = overlayClass?.perform(NSSelectorFromString("prepareDebuggingOverlay"))
let overlay = overlayClass?.perform(NSSelectorFromString("overlay")).takeUnretainedValue() as? UIWindow
_ = overlay?.perform(NSSelectorFromString("toggleVisibility"))

However you choose to do it, be sure that the code doesn’t make it into your App Store build, else you’re likely to get rejected.

但是如果你選擇這樣做,請確保代碼不會進入您的 App Store 構建,否則您可能會被拒審。

Update, May 26, 2017: Thanks to Bryce Pauken, who who discovered that once you’ve called [UIDebuggingInformationOverlay prepareDebuggingOverlay], you can just tap the status bar with two fingers to show the console. No need to call toggleVisibility.

更新,2017年5月26日:感謝 Bryce Pauken,發現當你打了 [UIDebuggingInformationOverlay prepareDebuggingOverlay] ,你可以用兩根手指點擊狀態欄來顯示控制臺。無需調用 toggleVisibility。

What UIDebuggingInformationOverlay can do

UIDebuggingInformationOverlay 可以做什么

As you can see from the above screenshot, the overlay provides seven features. I’ll give a brief overview of the first six. I wasn’t able to get the ‘System Color Audit’ screen to show me anything, send me a message if you have better luck.

從上面的屏幕截圖可以看出,覆蓋層提供了 7 種功能。我會簡要介紹一下前 6 個。我無法獲得 System Color Audit 屏幕來顯示任何內容,如果你做到了請聯系我。

The 'View Hierarchy' screen(視圖層次)


This screen shows what you’d probably expect; a list of views in the selected window. From here, you can inspect any view’s details, including its frame and instance variables. You can also switch between windows if you have more than one, or are just curious to see how a system window is built.

這個選項會展示選中的 window 上的 所有的 view,正如你期望的一樣。從這里,您可以檢查任何 view 的細節,包括 frame 和 實例變量。如果您有多個窗口,還可以在窗口之間切換,或者只是想知道如何構建系統窗口。

uidebugginginformationoverlay_viewhierarchyinfo.png

The 'VC Hierarchy' screen (控制器層次)


You can probably predict what this screen does as well. It’s very similar to the ‘View Hierarchy’ screen, except that it shows the hierarchy of active view controllers. From here, you can inspect any view controller’s details, including its view. You can also see whether the view controller is presenting a modal or is itself being presented.

您可以預測此屏幕的作用。 它與 View Hierarchy 功能非常相似,只是它顯示了 view controllers 的層次結構。 從這里,你可以查看任何視圖控制器的細節,包括它的 view。 您還可以看到 view controller 的狀態是 modalpresented。

uidebugginginformationoverlay_vchierarchy.png

The 'Ivar Explorer' screen


This screen gives you access to the UIApplication instance’s variables. More importantly, any object variables can also be explored. This includes the app delegate which, depending on your app’s structure, might provide a handy entry point into your custom objects.

這個選項可讓您訪問 UIApplication 的實例變量。 更重要的是,還可以探索任何對象變量。 包括 delegate,根據應用程序的結構,可以為您的自定義對象提供一個便利的訪問方式。

uidebugginginformationoverlay_ivarexplorer.png

The 'Measure' screen


This is one of the cooler features of the overlay, in my opinion. It lets you measure dimensions (in points) of the screen’s elements. First, select whether you want to see measurements on the ‘Horizontal’ or ‘Vertical’ axis. Then drag your finger along the screen, using the magnified viewer inside the console to assist you. There are two modes:

在我看來,這是覆蓋層的一個不常用的功能。它可以測量屏幕的尺寸(以點為單位)。 首先,選擇是否要在 水平垂直 軸上測量。 然后在屏幕上拖動手指,使用控制臺內的放大查看器來協助您。 有兩種模式:

The default mode ignores view boundaries. As far as I can tell, this mode treats the screen as a single rasterized image, and uses changes in color as boundaries for potential measurements. For example, in the screenshot below, I am able to measure the distance between the end of a label’s text and the edge of the screen:

默認模式忽略視圖邊界。 據我所知,此模式將屏幕視為單個光柵化圖像,并將顏色變化用作潛在測量的邊界。 例如,在下面的截圖中,我可以測量 Label 末尾與屏幕邊緣之間的距離:

uidebugginginformationoverlay_measure.png

“View mode”, on the other hand, displays the size of the subview in focus. Drag your finger over a view to see the selected axis’s dimension. In this screenshot, I’m measuring the height of the textfield at the top of the screen:

View mode 另一方面,可以顯示子視圖的大小。將手指拖動到視圖上查看所選方向的尺寸。這張截圖中,我在測量 textfield 的高度。

uidebugginginformationoverlay_measureview.png

The 'Spec Compare' screen


I could see this being an incredibly useful tool for collaboration between development and design. Add a screenshot to your device and then select it from the Spec Compare screen. The selected screenshot will be displayed on top of the live app. You can then drag down to decrease alpha and compare the spec to the actual implementation.

我可以看到這是開發和設計之間協作的一個非常有用的工具。 將屏幕截圖添加到設備,然后選擇 Spec Compare 。 所選屏幕截圖將顯示在 App 的頂部。 然后,您可以向下拖動以減少透明度,并將其與實際需求進行比較。

Update, May 26, 2017: Patrick Balestra reminded me that I left out an important step. You’ll need to include a value for the NSPhotoLibraryUsageDescription key in your Info.plist. Tapping the ‘Add’ button presents a UIImagePickerController
, and doing so without setting this value will cause your app to crash.

更新,2017年5月26日:Patrick Balestra 提醒我,我缺少重要的一步。 你需要在 Info.plist 中包含 NSPhotoLibraryUsageDescription 鍵的值。 點擊“添加”按鈕顯示一個 UIImagePickerController
,而不設置此值將導致您的應用程序崩潰。

uidebugginginformationoverlay_speccomparepicker.png
uidebugginginformationoverlay_speccompareoverlay.png

Wrap-up


I’ve only had a few days to play with this thing but am hoping to put it to use in our next beta. The console has a few rough edges, but it seems like a promising alternative to many of the open-source tools out there. If you have a chance to use it and notice anything that I missed, please feel free to reach out. I’ll update the post as I learn more.

我只玩了幾天玩這個東西,但希望在下一個測試版中使用它。 控制臺有一些粗糙的邊緣,但它似乎是許多開放源代碼工具的有前途的替代品。 如果您有機會使用它并注意到我沒注意到的地方,請隨時聯系。 我會更多的了解更多信息。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容