Bug when swiping from view with uinavigationbar to one without

淺唱寂寞╮ 提交于 2019-12-11 12:21:19

问题


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:


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.



来源:https://stackoverflow.com/questions/32756887/bug-when-swiping-from-view-with-uinavigationbar-to-one-without

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!