UINavigationBar和新的iOS 5 +外观API – 如何提供两个背景图像?

我想利用新的iOS 5外观API为我的应用程序中的所有UINavigationBar实例提供自定义背景图像。 要做到这一点,就像这样简单:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault]; 

但是,对于每个实例,我想根据translucent属性的值提供不同的图像,例如

 // For UINavigationBar instances where translucent returns YES: [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever-translucent.png"] forBarMetrics:UIBarMetricsDefault]; // Otherwise: [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"whatever.png"] forBarMetrics:UIBarMetricsDefault]; 

鉴于外观API似乎使用类方法configuration,是这样的可能吗?

目前,没有办法做你正在描述的东西 – 外观代理并不知道任何特定的实例在你要求的时候。

实际上,你可能需要做的是找出你有多less半透明的酒吧v你有多less非透明的酒吧。 select你有更多的,并使用外观代理的 – 其他人,当你去使其半透明(或要求全屏布局),那么你将不得不设置背景图像。

同时,你可以在http://bugreport.apple.com/上提出一个改进请求,询问你的问题吗? 这不是一个无理的要求。 谢谢!

您可以使用类外观代理全局设置它,或者将其设置在navBar的实例上。

我目前在导航栏的实例上设置背景,它似乎正在工作。 我有两个不同背景的navBars。 如果你在一个实例上设置它,你应该能够调整代码。

 UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController:myView]; [viewControllers addObject:myNavController]; // not supported on iOS4 UINavigationBar *navBar = [myNavController navigationBar]; if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { // right here, you could condition bg image based on properties of this instance // of the navBar or any other condition. [navBar setBackgroundImage:[UIImage imageNamed:@"bg.jpg"] forBarMetrics:UIBarMetricsDefault]; } 

如果你想使用类方法设置,你可以为所有设置:

 [[UINavigationBar appearance] setBackground ... 

我会承认,虽然这是相当新的,我只是想像大多数人一样。

这个答案对你来说可能不会有多大的帮助,但可能对别人有帮助。 如果你创build一个子类,你可以分别指定每个子类的外观。 例如,我有UITableviewCells和派生自UITableViewCells的自定义类。 我其实是这样做的原因,但我发现,我需要调用[[UITableViewCells外观] setFont:[…]]专为这两个类。

既然你似乎想要这样做,基于一个variables,直到运行时才会知道,你可能运气不好!

你可以这样做,如果你知道哪些类包含半透明的酒吧:

 [[UIBarButtonItem appearanceWhenContainedIn:[MyClassWithTranslucentBar class], [MyOtherClassWithTranslucentBar class], nil] setTintColor:desiredColor]; 

我不能留下评论,所以必须得到答案。 Rob Whitlow在这篇文章中写了一篇很棒的文章。 检查出来: http : //ios-blog.co.uk/tutorials/ios-custom-ui-series-tabbar-navbar/

尝试这个:

 if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { // Load resources for iOS 6.1 or earlier navigationController1 = [self customizedNavigationController]; [navigationController1 setViewControllers:[NSArray arrayWithObject: self.homeViewController]]; [self setNavigationController:navigationController1]; [self.window setRootViewController:navigationController]; } else { // Load resources for iOS 7 or later navigationController1 = [[UINavigationController alloc] initWithRootViewController:self.homeViewController]; [self.window setRootViewController:navigationController1]; } - (UINavigationController *)customizedNavigationController { UINavigationController *navController = [[UINavigationController alloc] initWithNibName:nil bundle:nil]; // Ensure the UINavigationBar is created so that it can be archived. If we do not access the // navigation bar then it will not be allocated, and thus, it will not be archived by the // NSKeyedArchvier. [navController navigationBar]; // Archive the navigation controller. NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:navController forKey:@"root"]; [archiver finishEncoding]; // Unarchive the navigation controller and ensure that our UINavigationBar subclass is used. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [unarchiver setClass:[LNTNavigationBar class] forClassName:@"UINavigationBar"]; UINavigationController *customizedNavController = [unarchiver decodeObjectForKey:@"root"]; [unarchiver finishDecoding]; // Modify the navigation bar to have a background image. LNTNavigationBar *navBar = (LNTNavigationBar *)[customizedNavController navigationBar]; [navBar setTintColor:[UIColor colorWithRed:0.39 green:0.72 blue:0.62 alpha:1.0]]; [navBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_1024_46.png"] forBarMetrics:UIBarMetricsDefault]; [navBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_1024_46.png"] forBarMetrics:UIBarMetricsLandscapePhone]; return customizedNavController; }