首页 > 代码库 > 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自定义时无法紧贴左侧