Separator between navigation bar and view - iOS 7

2019-02-21 14:03发布

问题:

In iOS 7 there's a grey separator between the navigation bar and the view.

Back in iOS 6, there wasn't that horizontal line, thus the view would blend in with the navigation bar as if they were the same image. Now I don't know how to remove it...

I've tried resizing the view / navigation bar, but it doesn't help. Any ideas?

回答1:

Try with

self.navigationController.navigationBar.translucent = NO;

In your viewDidLoad method and let me know :)

If you need this effect on every ViewController, you could simply do:

[[UINavigationBar appearance] setTranslucent:NO]

Or you'll need to do this where you first instantiate the navigation controller. For example, if the navigation controller is the root view controller of your app you can simply do

UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
nav.navigationBar.translucent = NO;

in your

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

If, on the other end, you instantiate it through a segue you could do (in the appropriate view controller)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if([segue.identifier isEqualToString:@"navController"]){     
       UINavigationController *nav = (UINavigationController *)segue.destinationViewController;
       nav.navigationBar.translucent = NO;
   }
}

And so on (if you're actually instantiating it from code, it should be the easiest option).



回答2:

The other answers did not work for me. To remove the separator, I had to set the background image AND the shadow image, like so:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];


回答3:

Add this:

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

in your AppDelegate.m in the application didFinishLaunchingWithOptions method