简体   繁体   中英

Programmatically hiding/unhiding status Bar in iOS 8 without animation

I am attempting to hide/unhide the status bar programmatically upon a view appearing and disappearing. This was my first attempt (that worked), but it animates my UINavigationBar which I don't want to happen.

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

But this is essentially what I want to work, but it does absolutely nothing.

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [super viewWillDisappear:animated];
}

Instead of using setStatusBarHidden:withAnimation: you should return UIStatusBarAnimationNone from

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation

and call

[self setNeedsStatusBarAppearanceUpdate];

Complete code:

@property (nonatomic, assign, readwrite) BOOL visible;

- (BOOL)prefersStatusBarHidden {
    return self.visible;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

- (void)setVisible:(BOOL)visible {
    _visible = visible;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.visible = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.visible = NO;
    [super viewWillDisappear:animated];
}

只需添加一条简单的线,您的问题就可以解决。

[[UIApplication sharedApplication] setStatusBarHidden:status];

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