如何更改导航栏背景

时间:2012-05-23 11:20:11

标签: objective-c ios xcode navigationbar

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

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];

}

我无法更改导航背景。

感谢。

3 个答案:

答案 0 :(得分:4)

这实际上比你想象的要简单。在你的app delegate ...

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

这将普遍改变您的导航栏背景图片。

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

答案 1 :(得分: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]];
}

// for image

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

答案 2 :(得分: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中编写此方法。我认为这会对你有所帮助。