首页 > 代码库 > iOS:navigationItem自定义时无法紧贴左侧
iOS:navigationItem自定义时无法紧贴左侧
解决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自定义时无法紧贴左侧
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。