UIBarButtonItem外观不能在设备上工作
问题描述:
我遇到了以下问题:UIBarButtonItem外观不能在设备上工作
我正在自定义我的应用程序的整个外观。现在我想更改导航栏中的后台按钮的背景。如果我在模拟器上运行我的代码,它可以工作。但是如果我在设备上运行它,它不会改变任何东西。这怎么可能?
我跑在我的iPhone 4S的iOS 5.0.1
#import "test.h"
#import "InfoButton.h"
@implementation test
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
//iOS 5 new UINavigationBar custom background
UIImage *navi = [UIImage imageNamed:@"navbar.png"];
[self.navigationController.navigationBar setBackgroundImage:navi forBarMetrics: UIBarMetricsDefault];
UIImage *toolbarBackButtonBackgroundPortrait = [[UIImage imageNamed:@"nav_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 17, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:toolbarBackButtonBackgroundPortrait forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
[super viewDidLoad];
UIButton * infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoDarkButtonType.frame = CGRectMake(0.0, 0.0, 30.0, 25.0);
infoDarkButtonType.backgroundColor = [UIColor clearColor];
[infoDarkButtonType addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:infoDarkButtonType];
self.navigationItem.rightBarButtonItem = infoButton;
[infoDarkButtonType release];
[infoButton release];
}
答
应用程序检查图像文件的拼写,尤其是盖帽。我注意到模拟器会忽略大写字母,并在设备区分大小写时仍然加载文件。我自己做了几次。
答
有同样的问题。我的图片被命名为“add.png”,并在代码中使用了[UIImage imageNamed:@“add.png”]。虽然这在模拟器中工作,但它不在设备上。
我将文件更名为“Add.png”(大写字母A),并在代码中更改为[UIImage imageNamed:@“Add.png”]。这解决了这个问题,并不适用于模拟器和设备 - 奇怪:/
非常感谢你,我将它保存在nav_bar.PNG下,现在它可以工作! – AmiiQo 2012-02-26 13:58:02