繁体   English   中英

如何在导航栏中添加图像

[英]How to add an image in the navigation bar

我想在导航栏中添加图像。 它应该在我的整个应用程序中。 应该将其添加到所有导航控制器中。

任何帮助将不胜感激。

尝试这个 ,

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

您可以使用以下代码添加图像。

UIImage *image = [UIImage imageNamed: @"yourImageName.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imgView;
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)])
    {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:
          [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
          UITextAttributeTextColor,
          [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
          UITextAttributeTextShadowColor,
          [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
          UITextAttributeTextShadowOffset,
          [UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
          UITextAttributeFont,
          nil]];
    }

把它放在您的Appdelegate.m中

您使用此代码:

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

我是一个理想的人,因为我遇到这样的问题,所以您可以尝试我的方式,OK!

首先,您创建一个导航控制器,然后在窗口上显示它

UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:YourView];

下一步:您要创建一个具有图像属性的对象Uiimageview,其中包含您要添加导航栏和图像的图像属性ptoperty是您想要的位置

UIImageView *imgView=[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImagename"]; imgView.Frame=CGrectmake();

最后一步:在导航栏中将对象imgView添加为子视图

[nav.navigationBar addSubview:imgView];

完成!!!!!!!!!!

暂无
暂无

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

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