2

我正在尝试使用此代码段为所有应用程序的左后退按钮导航栏设置图像背景:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0)
    {
        // iPhone 5.0 code here
        UIImage *image = [UIImage imageNamed: @"btn_back.png"];
         image = [image stretchableImageWithLeftCapWidth:40.0f topCapHeight:0.0f];
        [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    }
    return YES;
}

我一直在 stretchableImageWithLeftCapWidth: 中尝试不同的值:但最好的结果是:

在此处输入图像描述

如何将背景图像设置为正确的大小?

谢谢

4

1 回答 1

3

stretchableImageWithLeftCapWidth:topCapHeight:已弃用。改为使用resizableImageWithCapInsets:

这应该有效:

UIImage *buttonImage = [[UIImage imageNamed:@"btn_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 10, 1, 10)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
于 2012-05-08T17:49:01.260 回答