将图像添加到导航栏
我想让图像占据所有导航栏。这是基于导航的应用程序附带的导航。它出现在随附的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。最后,该解决方案看起来使用一个标签栏,我没有使用。设置导航栏背景很复杂?还有其他一些更简单的技术吗?
我想有一个导航的背景,仍然可以使用标题文本。
在你的情况下,this solution found in another answer会运作良好。
与添加到UINavigationBar的了“CustomImage”类别, 然后你可以只要致电:
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(否则会被多次添加...)
其实是有背景图像添加到任何一个更为简单的方法UIView
类或子类。它不需要类别分类或扩展(子类别),您可以在“根据需要”的基础上进行此操作。例如,背景图像添加到视图控制器的导航栏,做到以下几点:
self.navigationController.navigationBar.layer.contents = (id)[UIImage
imageNamed:@"background.png"].CGImage;
你需要记住的石英核心框架添加到您的项目,并添加#import <QuartzCore/QuartzCore.h>
无论你需要这样做。这是一种更简洁,更简单的方法,用于更改从UIView
继承的任何东西的绘图层。当然,如果你想为所有导航栏或标签栏实现类似的效果,那么子类化是有道理的。
UIImage *image = [UIImage imageNamed:@"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
使用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。米
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];
}
`
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];
它改变了iOS6的,使它在运行iOS 6中使用:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
只需添加这条线。
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
Bam!一行完成。
哈哈jk这是我最好的回答之一我在马的生活中看到的伊娃! – GangstaGraham 2013-08-23 10:31:00
刚去的视图控制器和超级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];
}
这对其他答案的改进如何? – Mark 2014-05-13 13:43:44
@yogesh你知道答案,但我认为代码看起来很漂亮,有一些评论/步骤。 – 2014-05-13 13:55:11
@Mark你可以添加这个超级视图didload和 – 2014-05-15 05:37:20
中的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];
}
完美。谢谢。另外,你知道我可以如何设置标题栏的颜色吗?我试过这个,但它不工作:[self.navigationController.navigationBar.titleView setBackgroundColor:[UIColor blueColor]]; – 4thSpace 2009-10-12 14:26:51
我也试过[navBar setBackgroundColor:[UIColor blueColor]]; – 4thSpace 2009-10-12 14:28:38
有tintColor属性:尝试navBar.tintColor = [UIColor blueColor]; – 2009-10-12 15:19:47