如何将背景图像应用于导航栏

时间:2013-08-05 11:53:02

标签: ios iphone uinavigationcontroller uinavigationbar

我想将图像插入导航栏。

我的代码是

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

我附上了当前输出的截图。这是因为图像尺寸较大吗?

enter image description here

但是我想要的输出是 enter image description here

8 个答案:

答案 0 :(得分:1)

首先,使您的导航栏图像大小为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];

答案 1 :(得分:0)

self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:UIBarMetricsDefault];

我希望,它适用于你

答案 2 :(得分:0)

使用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 { {1}}是height

答案 3 :(得分:0)

您可以设置导航栏的颜色与代码下方的图像使用相同,您可以将要显示的图像名称放在导航栏中

self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bodyBg.png"]];

答案 4 :(得分:0)

尝试使用此代码..

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

请参阅此链接setting-title-for-uinavigation-bar-in-iphone

的完整答案

答案 5 :(得分:0)

点击此链接,在navigationbar中添加背景图片,首先是navigationbar 图片大小设置为navigationbar

check this link here

我希望这段代码对你有用。

答案 6 :(得分:0)

[myNavbar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBackground.png"] 
           forBarMetrics:UIBarMetricsDefault];

以及此link

答案 7 :(得分:0)

试试这个适用于我的代码:

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