有没有办法更改导航项上的Bar Button项的宽度和插入? [英] Is there a way to change the width and insets of a Bar Button item on a Navigation item?

查看:114
本文介绍了有没有办法更改导航项上的Bar Button项的宽度和插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的MVC导航项目上的条形按钮项目上使用视网膜分辨率图像(40x40p)。当我设置图像时,它看起来像预期一样在水平方向拉伸:

I wanted to use a retina resolution image (40x40p) on a bar button item on my MVC's navigation item. When I set the image it looked stretched in horizontal direction as expected:

注意我还有一个工具栏按钮图像,其视网膜分辨率(60px60p)符合 Apple规范。当size参数具有默认值时,它被类似地拉伸,即width = 0,insets; t = 0,b = 0,l = 0,r = 0:

Note I also have a toolbar button image which is in retina resolution (60px60p) as required by the Apple spec. It was similarly stretched when the size parameters had default values, i.e. width = 0, insets; t=0,b=0,l=0,r=0:

然而,与导航器项目不同,在工具栏中,我可以从IB成功更改这些值。当我设置width = 30,并且插入t = 5,b = 5,l = 5,r = 5时,工具栏按钮完全显示:

However unlike the navigator item, in the toolbar case I could successfully change these values from IB. When I set width=30, and insets t=5,b=5,l=5,r=5 the toolbar button turned up perfectly:

当我使用尺寸时检查器并尝试设置导航项的条形按钮项的宽度和插入值,它似乎没有任何效果。齿轮图像看起来仍然很紧张。

When I used the Size inspector and tried to set the width and inset values of the navigation item's bar button item it seemed to have no effect. The gear image still looked stretched.

我还尝试按代码在导航项上创建条形按钮项,并在那里设置宽度和插入,它也不起作用:

I also tried creating the bar button item on the navigation item by code and set width and insets there, it didn't work either:

        UIImage *image = [UIImage imageNamed:@"073-Setting@2x.png"];
    //--------------------------------------------------------------------
    // This line had no effect for a bar button item on a navigation item:
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(5.0,5.0,5.0,5.0)];
    //--------------------------------------------------------------------
    UIBarButtonItem *button = [[UIBarButtonItem alloc] 
                               initWithImage:image 
                               style:UIBarButtonItemStylePlain
                               target:self
                               action:@selector(handleBack:)];
    //--------------------------------------------------------------------
    // This line had no effect for a bar button item on a navigation item:
    button.width = 30;
    //--------------------------------------------------------------------

任何想法我是否做错了,或者这看起来像一个错误。我的部署目标是IOS 5.0。

Any ideas whether I am doing something wrong, or this looks like a bug. My deployment target is IOS 5.0.

推荐答案

我现在明白了这个问题背后的原因。我不知道我需要在应用程序包中包含普通和高分辨率(@ 2x)图像。我天真地想要包含高​​分辨率图像并期望它能够工作。由于我只有iPhone故事板,包括捆绑中每个图像的20和40像素版本修复了我的问题。

I now understand the reason behind this issue. I did not know I needed to pack both normal and hi resolution (@2x) images within app's bundle. I was naively thinking to include hi-res image and expect it to work. Since I have iPhone story board only, including 20 and 40 pixel versions of each image in the bundle fixed my problem.

因此,无需使用按钮宽度或插图进行操作。此问题现已解决。

As a result there is no need to mack around with button widths or insets. This problem is fixed now.

这篇关于有没有办法更改导航项上的Bar Button项的宽度和插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆