我给导航条设置背景图片在didFinishLaunchingWithOptions 方法的代码:
UIImage *navBarImage;
navBarImage = [UIImage imageNamed:@"navbar.png"];
[[UINavigationBar appearance] setBackgroundImage: navBarImage forBarMetrics:UIBarMetricsDefault];
在app中添加一个选项,实现在切换时修改导航条的背景。但是没成功。是不是只有在应用开始之后才能设置背景图片?
我写出来的代码:
- (void) switchChanged:(id)sender {
UISwitch* switchView = sender;
if (switchView.isOn) {
UIImage *navBarImage = [UIImage imageNamed:@"black_nav.png"];
[[UINavigationBar appearance] setBackgroundImage: navBarImage forBarMetrics:UIBarMetricsDefault];
}
else {
UIImage *navBarImage = [UIImage imageNamed:@"white_nav.png"];
[[UINavigationBar appearance] setBackgroundImage: navBarImage forBarMetrics:UIBarMetricsDefault];
}
}