码迷,mamicode.com
首页 > 移动开发 > 详细

iOS:navigationItem自定义时无法紧贴左侧

时间:2014-11-26 14:28:53      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:uibutton   navigationitem   ios   

解决demo

<p class="p1"><span class="s1">UIButton</span><span class="s2"> *btn = [[UIButton alloc] initWithFrame:CGRectMake(</span><span class="s3">0</span><span class="s2">, </span><span class="s3">0</span><span class="s2">, BUTTON_WIDTH, BUTTON_HEIGHT)];</span></p><p class="p2"><span class="s4">    </span><span class="s2">//</span><span class="s5">添加点击事件</span></p><p class="p3"><span class="s4">    [btn </span><span class="s2">addTarget</span><span class="s4">:</span><span class="s6">self</span><span class="s4"> </span><span class="s2">action</span><span class="s4">:</span><span class="s6">@selector</span><span class="s4">(buttonClickAction:) </span><span class="s2">forControlEvents</span><span class="s4">:</span><span class="s2">UIControlEventTouchUpInside</span><span class="s4">];</span></p><p class="p2"><span class="s4">    </span><span class="s2">//</span><span class="s5">设置图片</span></p><p class="p3"><span class="s4">    [btn </span><span class="s2">setImage</span><span class="s4">:[</span><span class="s1">UIImage</span><span class="s4"> </span><span class="s2">imageNamed</span><span class="s4">:</span><span class="s7">@"naviationbar_button_normal.png"</span><span class="s4"> ] </span><span class="s2">forState</span><span class="s4">:</span><span class="s2">UIControlStateNormal</span><span class="s4">];</span></p><p class="p3"><span class="s4">    [btn </span><span class="s2">setImage</span><span class="s4">:[</span><span class="s1">UIImage</span><span class="s4"> </span><span class="s2">imageNamed</span><span class="s4">:</span><span class="s7">@"naviationbar_button_pressed.png"</span><span class="s4"> ] </span><span class="s2">forState</span><span class="s4">:</span><span class="s2">UIControlStateHighlighted</span><span class="s4">];</span></p><p class="p2"><span class="s4">    </span><span class="s2">//</span><span class="s5">设置</span><span class="s2">UIEdgeInsetsMake</span><span class="s5">(关键点)</span></p><p class="p1"><span class="s2">    btn.</span><span class="s1">imageEdgeInsets</span><span class="s2"> = </span><span class="s8">UIEdgeInsetsMake</span><span class="s2">(</span><span class="s3">0</span><span class="s2">, -</span><span class="s3">20</span><span class="s2">, </span><span class="s3">0</span><span class="s2">, </span><span class="s3">0</span><span class="s2">);</span></p><p class="p2"><span class="s4">    </span><span class="s2">//</span><span class="s5">设置</span><span class="s2">button</span><span class="s5">为</span><span class="s2">navigationItem</span></p><p class="p4"><span class="s4">    </span><span class="s2">UIBarButtonItem</span><span class="s4"> *backItem = [[</span><span class="s2">UIBarButtonItem</span><span class="s4"> </span><span class="s8">alloc</span><span class="s4">] </span><span class="s8">initWithCustomView</span><span class="s4">:btn];</span></p><p class="p4"><span class="s4">    </span><span class="s6">self</span><span class="s4">.</span><span class="s2">navigationItem</span><span class="s4">.</span><span class="s2">leftBarButtonItem</span><span class="s4"> = backItem;</span></p>

使用UIEdgeInsetsMake有些山寨,但是能达到效果就行了

iOS:navigationItem自定义时无法紧贴左侧

标签:uibutton   navigationitem   ios   

原文地址:http://blog.csdn.net/u013243469/article/details/41513747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!