实际上,没有必要调配方法、添加类别或使用 Interface Builder,相反,您可以子类化UINavigationBar
并使用NSKeyedUnarchiver
将类更改UINavigationController.navigationBar
为您的自定义子类。
+ (UINavigationController*)customizableNavigationControllerWithController:(UIViewController*)controller {
NSAssert(controller != nil, @"UINavigationController(CustomNavigationBar) customizableControllerWithController: controller can not be nil");
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:nil] autorelease];
NSData *navControllerData = [NSKeyedArchiver archivedDataWithRootObject:navController];
NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:navControllerData] autorelease];
[unarchiver setClass:[CustomNavigationBar class] forClassName:@"UINavigationBar"];
UINavigationController *customizableNavController = [unarchiver decodeObjectForKey:@"root"];
customizableNavController.viewControllers = [NSArray arrayWithObject:controller];
return customizableNavController;
}