【问题标题】:Adding image to navigation bar将图像添加到导航栏
【发布时间】:2010-12-05 21:41:50
【问题描述】:

我想要一张图片占据整个导航栏。这是基于导航的应用程序附带的导航。它与随附的 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。最后,该解决方案看起来使用了一个我没有使用的标签栏。设置导航栏背景有那么复杂吗?还有其他更简单的技术吗?

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

【问题讨论】:

    标签: iphone uitableview iphone-sdk-3.0 uiimageview uinavigationbar


    【解决方案1】:

    在你的情况下,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之前(否则会被添加多次...)

    【讨论】:

    • 完美。谢谢。另外,你知道如何设置标题栏颜色吗?我已经尝试过了,但它不起作用:[self.navigationController.navigationBar.titleView setBackgroundColor:[UIColor blueColor]];
    • 我也试过 [navBar setBackgroundColor: [UIColor blueColor]];
    • 有tintColor属性:试试navBar.tintColor = [UIColor blueColor];
    • 如果我不使用 setBackgroundImage 部分,标题文本颜色仍然不会随 tintColor 或 BackgroundColor 改变。我猜我们在更改该特定文本时处于错误的层。
    【解决方案2】:

    它已针对 ios6 进行了更改,以使其在 ios 6 中使用:

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

    【讨论】:

      【解决方案3】:
      UIImage *image = [UIImage imageNamed:@"YourImage.png"];
      UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
      
      [self.navigationController.navigationBar addSubview:imageView];
      

      【讨论】:

        【解决方案4】:

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

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

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

        【讨论】:

          【解决方案5】:
          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];
          

          【讨论】:

            【解决方案6】:

            只需添加这一行。

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

            砰!一行就搞定了。

            【讨论】:

              【解决方案7】:

              我使用 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 方法,但无法让它工作,所以我给图像一个标签,然后在视图将出现的其他视图控制器中删除它。

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

              `

              【讨论】:

                【解决方案8】:

                只需进入视图控制器并粘贴到 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];
                
                }
                

                【讨论】:

                • 这对这里的其他答案有何改进?
                • @yogesh 你知道答案,但我认为代码看起来很漂亮,有一些 cmets/steps。
                • @Mark 你可以在超级视图中添加这个 didload 和
                • @yogeshyadav 该评论没有添加任何内容,即评论重复了代码并且没有解释它 - 为什么以及它与其他答案的不同之处是它们是错误的还是效率较低?
                • @Mark 此代码仅在图像图标上显示导航标题我解释什么?
                【解决方案9】:

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

                【讨论】:

                  猜你喜欢
                  • 2012-08-15
                  • 1970-01-01
                  • 2015-03-09
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-03-20
                  • 1970-01-01
                  相关资源
                  最近更新 更多