2012-05-23 152 views
1

我尝试更改为导航栏的背景。如何更改导航栏背景

Search.h:

#import <UIKit/UIKit.h> 

@interface Search : UIViewController 

@end 

Search.m:

#import "Search.h" 

@implementation Search 

- (void)viewDidLoad 
{ 

[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

UIImage *image = [UIImage imageNamed:@"navigation_header.png"]; 

[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

} 

我不能改变导航的背景。

谢谢。

回答

4

这实际上比你想象的要简单。在你的应用程序代表...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault]; 
    return YES; 
} 

这将普遍改变你的导航栏背景图像。

注意:我相信UINavigationBar's“外观”属性可用于iOS 5及更高版本。

2

viewController.h

IBOutlet UINavigationBar *navBar; 

viewController.m //用于彩色

- (void)viewDidLoad{ 
[super viewDidLoad]; 
[navBar setTintColor:[UIColor colorWithRed:0.0/255.0 green:100.0/255.0 blue:20.0/255.0 alpha:1.0]]; 
} 

//用于图像

UIImage *image = [UIImage imageNamed:@"navigation_header.png"]; 
[self.navigationController.navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
+0

在'UINavigationController *'类型的对象上找不到属性'navBar' –

+0

使用此self.navigationController.navigationBar –

+0

现在没有错误,但背景不变。 –

0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    self.viewController = [[[Search alloc] initWithNibName:@"Search" bundle:nil] autorelease]; 
    navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
[self.window addSubview:[navControl view]]; 
[self.window makeKeyAndVisible]; 

}

在AppDelegate.m中编写此方法。我认为这对你会有所帮助。

相关问题