31

我想要一张图片占据整个导航栏。这是基于导航的应用程序附带的导航。它与随附的 UITableView 一起出现在 RootViewController 上。我已经看到了一些关于这可能如何工作的例子。

设置导航栏标题:

UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar.topItem setTitleView:imageView];

问题在于它只涵盖标题而不是整个导航栏。

还有这个线程:http ://discussions.apple.com/message.jspa?messageID=9254241#9254241 。最后,该解决方案看起来使用了一个我没有使用的标签栏。设置导航栏背景有那么复杂吗?还有其他更简单的技术吗?

我想要导航的背景并且仍然能够使用标题文本。

4

9 回答 9

36

在您的情况下,在另一个答案中找到的这个解决方案效果很好。

将“CustomImage”类别添加到 UINavigationBar,然后您可以调用:

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

这段代码应该在方法中

- (void)viewWillAppear:(BOOL)animated

您希望在其中拥有自定义图像的视图控制器。而且,在这种情况下,您最好致电:

[navBar clearBackgroundImage]; // Clear any previously added background image

在 setBackgroundImage 之前(否则会被添加多次...)

于 2009-10-12T08:27:55.187 回答
23

它针对 ios6 进行了更改,以使其在 ios 6 中使用:

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
于 2012-09-27T08:38:00.370 回答
9
UIImage *image = [UIImage imageNamed:@"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

[self.navigationController.navigationBar addSubview:imageView];
于 2011-01-11T05:47:51.937 回答
8

实际上有一种更简单的方法可以将背景图像添加到任何UIView类或子类。它不需要类分类或扩展(子类化),您可以“根据需要”执行此操作。例如,要将背景图像添加到视图控制器的导航栏,请执行以下操作:

self.navigationController.navigationBar.layer.contents = (id)[UIImage 
    imageNamed:@"background.png"].CGImage;

您需要记住将 Quartz Core 框架添加到您的项目中,并添加#import <QuartzCore/QuartzCore.h>到您需要执行此操作的任何位置。这是一种更简洁、更简单的方法来更改任何继承自UIView. 当然,如果你想为所有导航栏或标签栏实现类似的效果,那么子类化是有意义的。

于 2010-11-02T15:15:49.250 回答
4
UIImage *logo = [UIImage imageNamed:@"my_logo"];
UIImageView *logoView = [[UIImageView alloc]initWithImage:logo];
logoView.frame = CGRectMake(0, 0, 320, 37);

UINavigationController *searchNavCtrl = [[UINavigationController alloc] initWithRootViewController:searchViewController];
searchNavCtrl.navigationBar.barStyle = UIBarStyleBlack;


//searchNavCtrl.navigationItem.titleView = logoView;
//[searchNavCtrl.navigationController.navigationBar.topItem setTitleView:logoView];
[searchNavCtrl.navigationBar addSubview:logoView];

[logoView release];
于 2012-05-04T21:25:05.747 回答
4

只需添加这一行。

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];

砰!一行就搞定了。

于 2013-06-26T16:59:19.037 回答
3

我使用cmp的解决方案并添加了一些逻辑来删除它,因为我只希望在主屏幕上显示自定义背景图像。

主视图控制器.m

UIImage *image = [UIImage imageNamed:@"HomeTitleBG.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tag = 10;

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__);
    [testImgView removeFromSuperview];

} else {
    NSLog(@"%s no there isn't a bg image so add it ", __FUNCTION__);
}

[self.navigationController.navigationBar addSubview:imageView];


[imageView release];

我也尝试使用建议的 clearBackgroundImage 方法,但无法让它工作,所以我给图像一个标签,然后在视图上的其他视图控制器中删除它。

其他视图控制器.m

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];

if ( testImgView != nil )
{
    NSLog(@"%s yes there is a bg image so remove it", __FUNCTION__);
    [testImgView removeFromSuperview];  
} 

`

于 2011-04-30T15:34:48.140 回答
0

只需转到视图控制器并粘贴 super viewdidload 并在 mainlogo 中替换您的图像,然后在设置您的图像徽标中设置导航标题

  - (void)viewDidLoad
{
   [super viewDidLoad];

   //set your image frame
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,70,45)] ;

  //set your image logo replace to the main-logo
   [image setImage:[UIImage imageNamed:@"main-logo"]];

   [self.navigationController.navigationBar.topItem setTitleView:image];

}
于 2014-05-13T13:23:01.630 回答
0

在 appdelegate 中添加代码确实完成了启动方法

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    // Uncomment to assign a custom backgroung image
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];

    // Uncomment to change the back indicator image
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

    [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];

    // Uncomment to change the font style of the title

    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    shadow.shadowOffset = CGSizeMake(0, 0);

    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]];

    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
于 2015-09-14T09:36:27.850 回答