简体   繁体   English

如何在导航栏中添加图像

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

I want to add an image in the navigation bar. 我想在导航栏中添加图像。 It should be throughout my app. 它应该在我的整个应用程序中。 It should be added to all navigation controllers. 应该将其添加到所有导航控制器中。

Any help will be appreciated. 任何帮助将不胜感激。

Try this , 尝试这个 ,

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

you can add image with bellow code.. 您可以使用以下代码添加图像。

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

put this in your Appdelegate.m 把它放在您的Appdelegate.m中

您使用此代码:

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

i am an ideal , because i have got issue like this ,so you can try my way,ok!! 我是一个理想的人,因为我遇到这样的问题,所以您可以尝试我的方式,OK!

the first,you create an navigationcontroller and then show it on window 首先,您创建一个导航控制器,然后在窗口上显示它

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

next step : you create an object Uiimageview with image property with your image which you want to add navigationbar and frame ptoperty is posittion which you want 下一步:您要创建一个具有图像属性的对象Uiimageview,其中包含您要添加导航栏和图像的图像属性ptoperty是您想要的位置

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

last step: you add object imgView in navigationbar as subview 最后一步:在导航栏中将对象imgView添加为子视图

[nav.navigationBar addSubview:imgView];

Done!!!!!!!!!! 完成!!!!!!!!!!

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

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