为什么我的导航栏位于我的状态栏下?

时间:2010-07-11 07:36:13

标签: objective-c iphone cocoa-touch

http://www.irovr.com/stackOverflow/overlap.png

- (void)viewDidLoad {
        [super viewDidLoad];
        [self setWantsFullScreenLayout:YES];
        [mainScrollView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onMainScrollTap:)]];
    }

- (void)onMainScrollTap:(id)sender {
    if(self.navigationController.navigationBar.hidden){
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        [self.navigationController setToolbarHidden:NO animated:NO];
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    }else{
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController setToolbarHidden:YES animated:YES];
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.toolbar.translucent = YES;
}

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

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.toolbar.translucent = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController setToolbarHidden:YES animated:NO];
}

4 个答案:

答案 0 :(得分:11)

似乎同时隐藏状态栏和导航栏会导致此问题。我能够通过使用performSelector:withObject:afterDelay隐藏/显示导航栏来解决它,即使延迟为0

答案 1 :(得分:5)

使用“performSelector”将起作用。

但是,有些人可能会发现将以下内容添加到“viewWillDisappear”更容易,即使已经有一个取消隐藏导航栏的语句。

[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];

答案 2 :(得分:0)

您的视图设置得太大,因此它会在状态栏下方绘制。如果您使用Interface Builder将其创建为.xib,则需要在“模拟界面元素”下启用状态栏的设置,或者只是手动降低视图的高度。

答案 3 :(得分:0)

- (void)fixNavigationBarUnderStatusbarBug
{
    //This method fix bug! Don't cut it
    //Bug: Statusbar hide navigationBar after device rotation.
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
        [center addObserverForName:UIDeviceOrientationDidChangeNotification object:nil
                             queue:mainQueue usingBlock:^(NSNotification *note) {
                                 UIApplication *currentApplication = [UIApplication sharedApplication];
                                 if (currentApplication.statusBarHidden) {
                                     [currentApplication setStatusBarHidden:NO];
                                     double delayInSeconds = .1;
                                     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                                     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                                         [currentApplication setStatusBarHidden:YES];
                                     });
                                 }
                             }];
    }
}