我想在导航栏中插入图像。
我的代码是
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"bodyBg.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
我附上了当前输出的屏幕截图。这是因为图像尺寸大吗?

但我想要的输出是

我想在导航栏中插入图像。
我的代码是
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"bodyBg.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
我附上了当前输出的屏幕截图。这是因为图像尺寸大吗?

但我想要的输出是

首先,使您的导航栏图像大小为 1024x44 像素,视网膜显示为 2048x88 像素。
如果您在每个视图控制器上都有相同的 UINavigationBar 图像,请将其放在方法didFinishLaunchingWithOptions中的 AppDelegate 中:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav-background.png"] forBarMetrics:UIBarMetricsDefault];
// This will remove shadow in iOS6
if ([[UINavigationBar class] instancesRespondToSelector:@selector(shadowImage)]) {
[[UINavigationBar appearance] setShadowImage:[[[UIImage alloc] init] autorelease]];
}
而且我看到你需要自定义后退按钮,也把它放在 AppDelegate 中:
UIImage *backButtonNormal = [UIImage imageNamed:@"nav-back.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
试试这个对我有用的代码:
UIView* navigationBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImage * targetImage = [UIImage imageNamed:@"Header_top.png"];
UIGraphicsBeginImageContextWithOptions(navigationBGView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, navigationBGView.frame.size.width, navigationBGView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
navigationBGView.backgroundColor = [UIColor colorWithPatternImage:resultImage];
[self.navigationController.navigationBar addSubview:navigationBGView];
[self.navigationController.navigationBar setTintColor:[UIColor clearColor]];
[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
还有这个链接
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:UIBarMetricsDefault];
我希望它对你有用
使用UINavigationBar's setBackgroundImage: forBarMetrics:方法:
[self.navigationController.navigationBar setBackgroundImage:yourImageHere forBarMetrics:UIBarMetricsDefault];
你UIImage应该是的appropriate height and width。例如,iphone 4 的视网膜尺寸将是 640*88 的纵向。
编辑UIImage height:这里的要点是,如果bigger说 150 像素高度,它将显示那么多高度,而我们UINavigationBar height是44 pixel。
您可以将导航栏的颜色设置为与您的图像使用下面的代码相同,您可以将要显示在导航栏中的图像名称
self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bodyBg.png"]];
用这段代码试试..
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[navBar setBackgroundImage:[UIImage imageNamed:@"Nav.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:1];//any tag
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Nav.png"]];
[navBar insertSubview:imageView atIndex:0];
[imageView release];
}
}