我想将背景图片添加到uinavigationcontroller。请帮帮我。
答案 0 :(得分:14)
试试这个
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"yourImage"] forBarMetrics:UIBarMetricsDefault];
答案 1 :(得分:3)
使用此
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window makeKeyAndVisible];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"yourImage"] forBarMetrics:UIBarMetricsDefault];
return YES;
}
答案 2 :(得分:1)
swift 3和swift 4上的代码
UINavigationBar.appearance().setBackgroundImage(UIImage(named:"pattern.png"),
for: .default)
答案 3 :(得分:0)
如果要从ViewController更改导航栏图像
目标C
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img"] forBarMetrics:UIBarMetricsDefault];
快速
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"pi2"), for: .default)
如果要从Appdelegate更改导航栏图像
1. Using Objective C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"img"] forBarMetrics:UIBarMetricsDefault];
return YES;
}
2. Using swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().setBackgroundImage(UIImage(named:"img"), for:.default)
return true
}