简体   繁体   中英

How can I change navigationBar Background

I try change to background of 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];

}

I can't change navigation background.

Thanks.

This is actually simpler to do then you think. In your app delegate...

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

This will universally change your navigation bar background image.

Note: I believe UINavigationBar's "appearance" property is available in iOS 5 and up.

viewController.h

IBOutlet UINavigationBar *navBar;

viewController.m //for color

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

}

Write this method in AppDelegate.m. I think it will be helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM