简体   繁体   中英

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

ViewController.h file:

- (void)showForViewController:(UIViewController*)controller;

ViewController.m file:

- (void)showForViewController:(UIViewController *)controller {

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        self.providesPresentationContextTransitionStyle = YES;
        self.definesPresentationContext = YES;

        [self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    }
    else
    {
        [self setModalPresentationStyle:UIModalPresentationCurrentContext];
        [self.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];
    }

    [controller.navigationController presentViewController:self animated:NO completion:nil];
}

I'm using this code for present ViewController with clear background (to see previous controller).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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