Search code examples
iosobjective-cuinavigationcontrolleruinavigationbar

How to remove UINavigationController / UINavigationBar background in objective-c?


I have 2 ViewController and first one is rootviewcontroller for NavigationController. I want to remove background of navigationbar in second viewcontroller and just show my navigationitem at top. How to do that ? and what I should to do when I pop second viewcontroller, navigationbar have its old background not the secendview controller background ?

here is my code :

@implementation testView1

- (void)viewDidLoad
{
    [super viewDidLoad];
    [button addTarget:self action:@selector(ClickAction) forControlEvents:UIControlEventTouchUpInside   ];
    self.navigationController.navigationBar.translatesAutoresizingMaskIntoConstraints = false;
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"Home";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
}

-(void)ClickAction
{
    testView2 *detail =  [self.storyboard instantiateViewControllerWithIdentifier:@"V2"];
    [self.navigationController pushViewController:detail animated:YES];
}

@implementation testView2

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor lightGrayColor];
    self.title = @"No";
    self.navigationController.navigationBar.barTintColor = nil;
    self.navigationController.navigationBar.backgroundColor = nil;
    self.navigationController.navigationBar.tintColor = nil;
    self.navigationController.view.tintColor = nil;
    self.navigationController.view.backgroundColor = nil;

    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left-arrow.png"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    backButtonItem.imageInsets = UIEdgeInsetsMake(45, 5, 45, 85);
    self.navigationItem.leftBarButtonItem = backButtonItem;
}

-(void) Back:(id) sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

Solution

  • In firstcontroller

      -(void)viewWillAppear:(BOOL)animated
     {
      self.navigationController.navigationBar.translatesAutoresizingMaskIntoConstraints = false;
    self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"Home";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
      [super viewWillAppear:animated];
     }
    

    and write code in SecondView controller

     - (void)viewDidLoad 
     {
      [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                             forBarMetrics:UIBarMetricsDefault];
     self.navigationController.navigationBar.shadowImage = [UIImage new];
     self.navigationController.navigationBar.translucent = YES;
     self.navigationController.navigationBar.tintColor = [UIColor clearColor];
     self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
     }