【问题标题】:Is there a way to change the width and insets of a Bar Button item on a Navigation item?有没有办法更改导航项上条形按钮项的宽度和插图?
【发布时间】:2012-06-24 05:39:44
【问题描述】:

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

请注意,我还有一个工具栏按钮图像,其分辨率为 Apple spec 所要求的视网膜分辨率 (60px60p)。当尺寸参数具有默认值时,它也被类似地拉伸,即 width = 0, insets; t=0,b=0,l=0,r=0:

但与导航器项目不同的是,在工具栏的情况下,我可以成功地从 IB 更改这些值。当我设置 width=30 并插入 t=5,b=5,l=5,r=5 时,工具栏按钮完美出现:

当我使用大小检查器并尝试设置导航项的栏按钮项的宽度和插入值时,它似乎没有任何效果。齿轮图像看起来仍然拉伸。

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

        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。

【问题讨论】:

    标签: ios ios5


    【解决方案1】:

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

    因此,无需考虑按钮宽度或插图。这个问题现在已经解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 2011-08-14
      • 2021-03-15
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多