Adding image to navigation bar

后端 未结 9 2072
一个人的身影
一个人的身影 2020-12-23 02:09

I\'d like an image to take up all of a navigation bar. This is the navigation that comes with a navigation based app. It appears on the RootViewController with the accompa

相关标签:
9条回答
  • 2020-12-23 03:04

    Just add this line .

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

    Bam! One line and done.

    0 讨论(0)
  • 2020-12-23 03:06

    I used cmp's solution and added some logic to remove it as I only wanted a custom background image on home screen within on view appear.

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

    I also tried to use the suggested clearBackgroundImage method but couldn't get it to work so I gave the image a tag and then removed it in the other viewcontrollers on view will appear.

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

    `

    0 讨论(0)
  • 2020-12-23 03:07

    just go the view controller and paste in super viewdidload and replace your image in mainlogo and then set the navigation title in set your image logo

      - (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];
    
    }
    
    0 讨论(0)
提交回复
热议问题