為autoLayout 增加識別符號,方便除錯
阿新 • • 發佈:2018-11-10

如上圖,是一個十分簡單的佈局。 root view 上加了一個 button 和一個 webview。
不加識別符號的樣子
檢視層級中沒有標識

只有 UIView、WKWebView 之類,如果view很多,就很難分得清。約束沒有識別符號 
只知道 uiview 和uibutton 中心對齊,不知道哪個view 和哪個 button。
autoLayout 日誌中沒有識別符號
(lldb) po [0x7f8aaa50e6d0 _autolayoutTrace] •UIWindow:0x7f8aaa60a1b0 | •UIView:0x7f8aaa502c30 | | *_UILayoutGuide:0x7f8aaa50d140 | | *_UILayoutGuide:0x7f8aaa50d750 | | *UIButton:0x7f8aaa50e6d0'click to send' | | | UIButtonLabel:0x7f8aaa6073b0'click to send' | | *WKWebView:0x7f8aaa851000 | | | WKScrollView:0x7f8aaa853a00 | | | | WKContentView:0x7f8aaa83c400 | | | | | UIView:0x7f8aaa706350 | | | | | | UIView:0x7f8aaa7002e0 | | | | | | | WKCompositingView:0x7f8aaa712200 | | | | | | | | WKCompositingView:0x7f8aaa60ec10 | | | | | | | | | WKCompositingView:0x7f8aaa60edf0 | | | | | | | | | | WKCompositingView:0x7f8aaa60f1b0 | | | | | | | | | | | WKCompositingView:0x7f8aaa60efd0 | | | | | | | | | | WKCompositingView:0x7f8aaa60e850 | | | | | | | | | WKCompositingView:0x7f8aaa60e670 | | | | | | | | WKCompositingView:0x7f8aaa60ea30 | | | | UIView:0x7f8aaa7066b0 | | | | UIImageView:0x7f8aaa70ce00 | | | | UIImageView:0x7f8aaa70cbd0 Legend: * - is laid out with auto layout + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES • - layout engine host (lldb) po [0x7f8aaa50e6d0 constraintsAffectingLayoutForAxis:0] <__NSArrayI 0x600000f3aa30>( <NSContentSizeLayoutConstraint:0x6000025ac2a0 UIButton:0x7f8aaa50e6d0'click to send'.width == 102 Hug:250 CompressionResistance:750 (active)>, <SnapKit.LayoutConstraint:
)
為約束增加識別符號
button.snp.makeConstraints { (make) in make.centerX.equalToSuperview().labeled("mybutton centerX") make.top.equalToSuperview().offset(50).labeled("mybutton top") } webView.snp.makeConstraints({ (make) in make.leading.trailing.bottom.equalToSuperview().labeled("mywebview Edges") make.top.equalToSuperview().offset(200).labeled("mywebview Top") })
效果
檢視層級中,可以看到約束的識別符號了。 
選中約束之後,可以看到 Identifier了

layout日誌中,可以看到識別符號了 
為 view 設定 accessibilityIdentifier
屬性
view.accessibilityIdentifier = "rootView abi"
button.accessibilityIdentifier = "mybtn abi"
webView.accessibilityIdentifier = "mywebview abi"
效果
layout 日誌中可以看到 

但是 snapKit 約束的描述,設定的accessibilityIdentifier
增加 accessibilityLabel

在檢視層級中,可以看到
總結
- 設定約束的識別符號,無論是 SnapKit,還是系統原生的
- 設定
accessibilityIdentifier
屬性,在日誌中可以幫助定位問題 - 設定
accessibilityLabel
屬性,幫助檢視檢視成績