繁体   English   中英

将图像添加到导航栏

[英]Adding image to navigation bar

我想要一个图像占据所有的导航栏。 这是基于导航的应用程序附带的导航。 它出现在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 接近最后,该解决方案看起来使用了一个我不使用的标签栏。 设置导航栏背景是否复杂? 还有其他一些更简单的技术吗?

我想有一个导航背景,仍然可以使用标题文本。

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

将“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之前(否则会多次添加...)

它改为ios6,使其在ios 6中使用:

UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:@"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed:@"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

[self.navigationController.navigationBar addSubview:imageView];

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

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

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

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

只需添加此行即可。

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

巴姆! 一行完成。

我使用了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];  
} 

`

只需转到视图控制器并粘贴到超级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];

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM