iOS导航栏隐藏2种方式

方法一

/此种方式隐藏导航栏一般应用于首页,因为此种方式会导致返回按钮消失

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
    //设置状态栏的颜色(如果不需要设置,可以忽略)
    if ([[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"] respondsToSelector:@selector(setBackgroundColor:)]) {
        [[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"] setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]];
    }
    
    //如果状态栏背景为浅色,应选用黑色字样式(UIStatusBarStyleDefault,默认值);如果背景为深色,则选用白色字样式(UIStatusBarStyleLightContent)。
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
    //将状态栏的颜色改回来
    if ([[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"] respondsToSelector:@selector(setBackgroundColor:)]) {
        [[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"] setBackgroundColor:[UIColor blueColor]];
    }
}


方法二

#import "MyViewController2.h"
#import "AppDelegate.h"

@interface MyViewController2 ()
@property(strong,nonatomic)UIImageView* barImageView;
@end

@implementation MyViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    // Do any additional setup after loading the view.

    //为了防止push,pop是,显示短暂的黑色
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.window.backgroundColor = [UIColor whiteColor];

    self.barImageView = self.navigationController.navigationBar.subviews.firstObject;

}

//此种导航栏的隐藏是改变图片的透明度,还可以用来做导航栏颜色根据tableView的偏移量动态改变

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _barImageView.alpha = 0 ;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _barImageView.alpha = 1 ;
    
}

@end

Demo地址https://github.com/JnLuffy/LFY_IOS_DEMOLIST/tree/master/LFY_NavigationBar_Demo

你可能感兴趣的:(iOS导航栏隐藏2种方式)