简体   繁体   中英

Black Translucent Navigation Bar Flashes Opaque on First Push

I have a Navigation Controller where I set the navigationBar.barStyle = UIBarStyleBlack and the navigationBar.translucent = YES (as per Apple's advice since they deprecated UIBarStyleBlackTranslucent). In my two nib files (this is not using storyboard) in the simulated metrics I have Top Bar set to Black Navigation Bar.

SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];

controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeSettings)];

controller.navigationController.navigationBar.barStyle = UIBarStyleBlack;
controller.navigationController.navigationBar.translucent = YES;

[self presentViewController:navController animated:YES completion:nil];

When I present the navigation controller, it opens up with the black translucent bar properly, but when I then push to the next table view, the navigation bar quickly fades to opaque and then back to translucent in the course of about 200ms. Its pretty much flashing opaque then back to translucent.

When I then push to the next table view, or go back (either by pressing the button in the top left of the nav bar, or by popping the view) it doesn't flash. It stays translucent the whole way through until the whole navigation controller is dismissed.

I thought this might be because of the way the nib was set up with an opaque bar, but I have tried every type of option (translucent, the regular blue, no bar) and it still does it.

Also, it does this across both completely separate navigation controllers in my app. Sorry if I am doing something obviously wrong, but I have tried so many combinations of options and am just at a loss.

Thanks!

I think you shouldn't use BarStyle at all but just :

[controller.navigationController.navigationBar setTintColor:[UIColor blackColor]];
[controller.navigationController.navigationBar setTranslucent:YES];

I would also try to animate the style change to get a smoother one with that in the presented view controller viewDidAppear method (think to begin with a black opaque bar in your code):

...
// Keep that where you want

SettingsViewController *controller = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];

controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeSettings)];

// remove the two lines

[self presentViewController:navController animated:YES completion:nil];

}

Then in the SettingViewController code put :

- (void) viewDidAppear:(BOOL)animated {
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:duration];

    [controller.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    [controller.navigationController.navigationBar setTranslucent:YES];

    [UIView commitAnimations];
}

Set appropriate value to your UIViewController instance property edgesForExtendedLayout , and set backgroundColor to your navigationController , for example:

self.edgesForExtendedLayout = UIRectEdgeBottom;
self.navigationController.view.backgroundColor = [UIColor whiteColor];

Hope this can help you.

SWIFT 5 VERSION

    self.edgesForExtendedLayout = UIRectEdge.bottom
    self.view.backgroundColor = .white

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