Ios 如果隐藏状态栏,然后使其重新出现,则导航栏';s的位置向上移动

Ios 如果隐藏状态栏,然后使其重新出现,则导航栏';s的位置向上移动,ios,Ios,我有一个问题,这是由于状态栏被隐藏,然后重新出现-这样做会将导航栏的位置从显示在状态栏下方更改为显示在状态栏后面。i、 e.导航栏的顶部不再可见,因为状态栏位于其顶部 我的程序是这样构造的:我有一个根视图控制器,显示状态栏和导航栏,视图控制器包含在导航控制器中,即 UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.myTableViewCo

我有一个问题,这是由于状态栏被隐藏,然后重新出现-这样做会将导航栏的位置从显示在状态栏下方更改为显示在状态栏后面。i、 e.导航栏的顶部不再可见,因为状态栏位于其顶部

我的程序是这样构造的:我有一个根视图控制器,显示状态栏和导航栏,视图控制器包含在导航控制器中,即

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.myTableViewController];
self.window.rootViewController = navController;
视图控制器包含一个表视图,如果用户选择一行,则会创建另一个控制器并将其推送到导航控制器的堆栈中

这个新的视图控制器隐藏状态栏,也隐藏导航栏,除非用户点击屏幕,在这种情况下,导航栏会出现(它也是半透明的,不在父视图控制器中)

我的问题是,当用户从该视图控制器导航回表视图控制器时,导航栏现在显示在状态栏后面

在表视图的视图中将出现以下代码:我认为应该将所有内容重置为原来的状态

- (void)viewWillAppear:(BOOL)animated
{    
    [[UIApplication sharedApplication] setStatusBarHidden: NO withAnimation: UIStatusBarAnimationFade];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
当表格视图重新显示时,为什么导航栏显示在状态栏后面


谢谢

我相信状态栏大约有20px高,所以将导航栏的origin.y设置为20,它应该会修复它。我知道这很烦人,但从我的经验来看,这似乎是解决问题的唯一办法


同时检查导航栏的位置当你的应用程序脱离后台状态时,我知道我的导航栏在进入前台时会恢复到状态栏下。

我遇到了同样的问题,在尝试了许多关于堆栈溢出和测试的goole之后,找到了解决问题的方法。我认为问题是由视图的边界更改引起的(例如,当您隐藏状态栏时,视图的边界将更改)

(在我的项目中,不仅隐藏状态栏,还隐藏导航控制器的导航栏。)

需要以下代码:

- (id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:UIApplicationWillEnterForegroundNotification object:nil];
    }
    return self;
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)test
{
    // the point is UIApplicationWillEnterForegroundNotification notification process
    // function should be called before all view controller delegate function being 
    // called, then we make all status bar and navigation bar show to restore the view's 
    // bounds, view contrller's view's bound should correct, then hide will not shift up
    [self showStatusBarAndNaviBar];
    [self performSelector:@selector(hideStatusBarAndNaviBar) withObject:self afterDelay:1.0];
}

//two custom function for use
- (void)hideStatusBarAndNaviBar
{
    [UIView animateWithDuration:2.0f
                      delay:0.0f
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^(void) {
                     self.navigationController.navigationBar.alpha = 0.0f;
                     [[UIApplication sharedApplication] setStatusBarHidden:YES];
                 }
                 completion:^(BOOL finished){}];
}

- (void)showStatusBarAndNaviBar
{
    [UIView animateWithDuration:1.0f
                      delay:0.0f
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^(void) {
                     self.navigationController.navigationBar.alpha = 1.0f;
                     [[UIApplication sharedApplication] setStatusBarHidden:NO];
                 }
                 completion:^(BOOL finished){}];
}