Solução independente como uma categoria Objective-C:
UINavigationController + TransparentNavigationController.h
@interface UINavigationController (TransparentNavigationController)
- (void)presentTransparentNavigationBar;
- (void)hideTransparentNavigationBar;
@end
UINavigationController + TransparentNavigationController.m
#import "UINavigationController+TransparentNavigationController.h"
@implementation UINavigationController (TransparentNavigationController)
- (void)presentTransparentNavigationBar
{
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:YES];
[self.navigationBar setShadowImage:[UIImage new]];
[self setNavigationBarHidden:NO animated:YES];
}
- (void)hideTransparentNavigationBar
{
[self setNavigationBarHidden:YES animated:NO];
[self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
@end
Agora, você pode importar a categoria no seu UIViewController
e chamar os métodos no seu controlador de navegação - por exemplo:
#import "UINavigationController+TransparentNavigationController.h"
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController presentTransparentNavigationBar];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController hideTransparentNavigationBar];
}
E uma solução semelhante no Swift :
import Foundation
import UIKit
extension UINavigationController {
public func presentTransparentNavigationBar() {
navigationBar.setBackgroundImage(UIImage(), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = true
navigationBar.shadowImage = UIImage()
setNavigationBarHidden(false, animated:true)
}
public func hideTransparentNavigationBar() {
setNavigationBarHidden(true, animated:false)
navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(UIBarMetrics.Default), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = UINavigationBar.appearance().translucent
navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
}
}
self.edgesForExtendedLayout = UIRectEdgeNone;