弹出视图时未恢复自定义导航栏背景
我在导航工具栏中遇到问题。我在我的应用程序委托中设置了导航工具栏背景图像,然后有一个按钮,单击它时会更改导航工具栏背景图像并移动到另一个视图。弹出视图时未恢复自定义导航栏背景
但是,当我点击后退按钮并返回到原始视图时,导航工具栏背景图像不会改回。
下面是相关代码:
在我的第一个视图控制器:
- (void)viewDidLoad
{
[super viewDidLoad];
//set to change the navigation toolbar to this background image (the first) when this view loads
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"QuickGrocery Logo Updated.jpg"] forBarMetrics:UIBarMetricsDefault];
}
按钮动作:
- (IBAction)wellBalancedMealsClicked:(id)sender {
QuickGroceryMasterViewController *masterViewController = [[QuickGroceryMasterViewController alloc] initWithNibName:@"QuickGroceryMasterViewController" bundle:nil];
[masterViewController setRecipeChoice:1];
//changes the navigation toolbar background image
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Keep It Balanced.jpg"] forBarMetrics:UIBarMetricsDefault];
}
有导航栏只是一个图像。如果你改变它,并且以后你想让它改回来,你必须自己改变它。
我建议你给每个视图控制器一个navigationBarImage
属性。为您的导航控制器设置代表。在委托中,添加这个方法:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIImage *image = [(id)viewController navigationBarImage];
if (image)
[navigationController.navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
}
罗布,感谢您的帮助。如何为导航控制器“设置委托人”? – user1072337
将导航控制器的'delegate'属性设置为实现'navigationController:willShowViewController:'的类的实例。 –
[可可核心竞争力:授权](http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html) –
你的代码是不工作的原因是'回到你的第一个视图控制器时viewDidLoad'不叫。只有从视图控制器的视图加载视图控制器的视图时,才会调用此方法。 Rob Mayoff的回答是最好的解决方案。 – jrturton