我想要一张图片来占据所有导航栏。这是基于导航的应用程序附带的导航。它出现在RootViewController上,附带UITableView。我已经看到了一些如何运作的例子。
设置导航栏标题:
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。接近最后,该解决方案看起来使用了一个我不使用的标签栏。设置导航栏背景是否复杂?还有其他一些更简单的技术吗?
我希望有导航背景,仍然可以使用标题文字。
答案 0 :(得分:36)
在您的情况下,this solution found in another answer会运作良好。
将“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之前(否则会多次添加...)
答案 1 :(得分:23)
对ios6进行了更改,使其在ios 6中使用:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
答案 2 :(得分:9)
UIImage *image = [UIImage imageNamed:@"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
答案 3 :(得分:8)
实际上有一种更简单的方法可以将背景图像添加到任何UIView
类或子类中。它不需要类分类或扩展(子类化),您可以在“根据需要”的基础上执行此操作。例如,要将背景图像添加到视图控制器的导航栏,请执行以下操作:
self.navigationController.navigationBar.layer.contents = (id)[UIImage
imageNamed:@"background.png"].CGImage;
您需要记住将Quartz Core框架添加到项目中,并在需要的地方添加#import <QuartzCore/QuartzCore.h>
。这是一种更简洁,更简单的方法来更改从UIView
继承的任何内容的绘图层。当然,如果你想为所有导航栏或标签栏完成类似的效果,那么子类化是有意义的。
答案 4 :(得分: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];
答案 5 :(得分:4)
只需添加此行。
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
的Bam!一行完成。
答案 6 :(得分:3)
我使用了 cmp 的解决方案并添加了一些逻辑来删除它,因为我只想在视图中的主屏幕上显示自定义背景图像。
HomeViewController.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方法,但无法使其工作,所以我给图像一个标签,然后在视图中的其他viewcontrollers中将其删除。
OtherViewController.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];
}
`
答案 7 :(得分: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];
}
答案 8 :(得分: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];
}