代码之家  ›  专栏  ›  技术社区  ›  horseshoe7

通过外观为UIBarButtonItem设置背景图像不适用于其他UIControlStates

  •  1
  • horseshoe7  · 技术社区  · 12 年前

    我正在尝试使用外观方法(>iOS 5.0)自定义UIBarButtonItem。它适用于UIControlStateNormal,但不适用于高亮显示或禁用。查看图片

    Ok in the default state, notice the disabled state at right

    highlighted state also not working

    以下是我用来设置这些的代码:

            // now configure the UIBarButtonItems
        UIImage *buttonBGInactive = [[UIImage imageNamed:@"button-navbar-30-inactive.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
        UIImage *buttonBGActive = [[UIImage imageNamed:@"button-navbar-30-pressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 15)];
    
        [[UIBarButtonItem appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setBackgroundImage: buttonBGInactive forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [[UIBarButtonItem appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setBackgroundImage: buttonBGActive forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    

    有什么想法吗?图片本身是30x30px,我不需要横向模式。

    1 回复  |  直到 12 年前
        1
  •  2
  •   horseshoe7    12 年前

    我找到了解决问题的办法。我的图像是30x30px,并且我将可调整大小的收头设置为

    UI边缘嵌入Make(0,15,0,15)

    通过将这15个设置为14个,问题就消失了。因此,最好不要使端盖恰好是图像宽度的一半。