如何设置导航控制器背景

how to set a navigation controllers background

如何将图像设置为导航控制器的背景,如视图,而不是栏。

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background.jpg"] forBarMetrics:UIBarMetricsDefault];

我尝试使用它来设置它,甚至将 属性 更改为 UIViewControllers,但在那之后。它只是吐出错误。

为什么要更改导航控制器的背景?它处理您想要在其间导航的两个控制器之间的转换,它本身不用作控制器 - 它实际上只是导航栏组件。如果要更改背景,请在初始视图和最终视图中进行更改。

在您的 AppDelegate 中 - (void) applicationDidFinishLaunching:(UIApplication *)application 方法添加以下内容。

[[UINavigationBar appearance] setBackgroundImage:YourImage forBarMetrics:UIBarMetricsDefault];

在Swift

let navBackgroundImage:UIImage! = UIImage(named: "backgroundNB.png")
UINavigationBar.appearance().setBackgroundImage(navBackgroundImage,  forBarMetrics: .Default)