顶部导航栏 发表于 Aug 12 2017 简介项目中经常会碰到顶部导航栏需要隐藏的情况,这里给出三种方法。第一种简单粗暴直接上来就隐藏掉导航栏,在基类中自己定义view充当导航栏的作用第二种1234567891011- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated];}- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.navigationController setNavigationBarHidden:NO animated:animated];} 这里一定要用动画的方式隐藏导航栏,这样在使用滑动返回手势的时候效果最好,和上面动图一致.这样做有一个缺点就是在切换tabBar的时候有一个导航栏向上消失的动画。第三种做法(推荐)12345678910111213141516171819202122232425@interface HomePageController () <UINavigationControllerDelegate>@end@implementation HomePageController - (void)viewDidLoad { [super viewDidLoad]; // 设置导航控制器的代理为self self.navigationController.delegate = self;}#pragma mark - UINavigationControllerDelegate// 将要显示控制器- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { // 判断要显示的控制器是否是自己 BOOL isShowHomePage = [viewController isKindOfClass:[self class]]; [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];}- (void)dealloc { self.navigationController.delegate = nil;} 注意(最后要记得在控制器销毁的时候把导航栏的代理设置为nil)