您的位置:首页 > 其它

使用Xcode 自带的view hierarchy 来分析view的层次和属性

2015-08-07 12:46 399 查看
最近发现Xcode的view hierarchy debug挺酷炫的,写个简单的材料

新建一个工程,然后在模拟器上运行,代码就是在viewDidLoad里面生成一个button1,设置背景颜色和文字。

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">- (<span style="color: #bb2ca2">void</span>)viewDidLoad {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000">   [</span><span style="color: #bb2ca2">super</span><span style="color: #000000"> </span>viewDidLoad<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    <span style="color: #703daa">UIButton</span> *button1=[[<span style="color: #703daa">UIButton</span> <span style="color: #3d1d81">alloc</span>]<span style="color: #3d1d81">initWithFrame</span>:<span style="color: #3d1d81">CGRectMake</span>(<span style="color: #272ad8">100</span>,<span style="color: #272ad8">100</span>, <span style="color: #272ad8">200</span>, <span style="color: #272ad8">200</span>)];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color: #000000">    button1.</span>backgroundColor<span style="color: #000000">=[</span>UIColor<span style="color: #000000"> </span><span style="color: #3d1d81">blueColor</span><span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000">    [button1 </span>setTitle<span style="color: #000000">:</span><span style="color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">点击</span><span style="color: #d12f1b">"</span><span style="color: #000000"> </span>forState<span style="color: #000000">:</span>UIControlStateNormal<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    [<span style="color: #bb2ca2">self</span>.<span style="color: #703daa">view</span> <span style="color: #3d1d81">addSubview</span>:button1];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: #000000">    [button1 </span>addTarget<span style="color: #000000">:</span><span style="color: #bb2ca2">self</span><span style="color: #000000"> </span>action<span style="color: #000000">:</span><span style="color: #bb2ca2">@selector</span><span style="color: #000000">(test) </span>forControlEvents<span style="color: #000000">:</span>UIControlEventTouchUpInside<span style="color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color: #000000">    </span>// Do any additional setup after loading the view, typically from a nib.</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">}</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">-(<span style="color: #bb2ca2">void</span>)test{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">    <span style="color: #3d1d81">NSLog</span>(<span style="color: #d12f1b">@"test"</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;">}</p>

程序运行后如下图所示,然后在xcode中选择 Debug -> View Debugging -> Capture View Hierarchy



在xcode中就会看到view 的层次图



适当的调整后可以看到



在控制台可以使用(lldb)的命令 输出指定内存地址的相关属性,字体,大小,颜色。

当前选中的UIButton的约束也是可以看到的,还有button的target是ViewController 

如果是较复杂的视图还可以调整可见的层次,总归是个利器。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: