Bug when swiping from view with uinavigationbar to

2019-08-08 20:11发布

I have an application with 2 viewcontrollers, ViewController and NextViewController. ViewController does not have a navigation bar and has a white status bar. NextViewController does have a navigation bar and has a black (default) status bar. I have encountered a bug when swiping back to ViewController from NextViewController and cancelling the swipe where the navigation bar on NextViewController will disappear. The storyboard simply has the two views with a button and an action segue. The bug does not always happen, but often enough to be a problem. The bug has to do with the method - (UIStatusBarStyle) preferredStatusBarStyle, as everything works fine when I remove the methods.

Can I somehow stop this bug from happening while still having a white status bar on my ViewController and having the swipe enabled or am I forced to remove either functionality?

ViewController

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
}

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

- (UIStatusBarStyle) preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

NextViewController

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}

Other possibly relevant info: Experiencing the issue on an iPhone 5s with iOS 8, but it also happens on the simulator with iOS 9. View controller-based status bar appearance is set to the default, YES.

1条回答
Root(大扎)
2楼-- · 2019-08-08 20:29

Status bar style depends on - preferredStatusBarStyle of the view controller if the navigation bar is hidden, otherwise of the navigation controller, so the - preferredStatusBarStyle in your NextViewController is useless.

It seems that the bug is related with the status bar style of the navigation controller, you can subclass UINavigationController override the method childViewControllerForStatusBarStyle to avoid the bug.

@implementation MyNavigationController

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

@end

Now the status bar style will depend on the view controller no matter whether the navaigation bar is hidden or not. And the bug would not happend.

查看更多
登录 后发表回答