2011-03-08 49 views
10

setStatusBarHidden:NO設置之前視圖負載,UINavigationBar和其他元素立即出現狀態條下方對齊,他們應該。但是,當setStatusBarHidden:NO設置爲視圖加載,UINavigationBar部分覆蓋。setStatusBarHidden:否之後XIB負載覆蓋UINavigationBar的

加載所述視圖後必須顯示StatusBar,但如何在不遇到上述問題的情況下完成此操作?

+0

我很想知道這個問題的答案爲好。 – 2011-06-24 17:48:05

回答

16

我在我的代碼中發現了一個黑客,雖然不記得或找到它來自哪裏。訣竅是通過隱藏和reshowing以刷新導航欄:

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

在我的代碼的功能如下:

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

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; 

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

然而,被警告,這是一個黑客,目前我正在努力解決一些似乎源於此代碼的錯誤(導航條目與導航內容不匹配)。但是因爲它在某些地方對我有用,所以我想我會提及它。

編輯: 我想我找到了最初的帖子在這裏: How do I get the navigation bar in a UINavigationController to update its position when the status bar is hidden?

GL, 俄德

+0

這用於工作,但它看起來像不在iOS 5上。:( – davehayden 2012-01-30 22:27:24

1

我的猜測是導航欄在狀態欄顯示之前加載,因此導航欄的位置是(0,0),然後與(0,0)處的狀態欄重疊。在致電setStatusBarHidden:NO後,您可以移動導航欄的框架(或設置動畫塊)viewDidLoad。 嘗試做navigationBar.frame = CGRectMake(0,20,320,44); 狀態欄是320x20,所以只需將導航欄向下移動20就應該適應它。

0

下面是我在做什麼在我的根控制器現在在iOS 5中,我告訴後狀態欄動畫英寸醜,但它似乎工作。

CGRect rect; 

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) 
    rect = CGRectMake(0, 20, 320, 460); 
else if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    rect = CGRectMake(0, 0, 320, 460); 
else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    rect = CGRectMake(20, 0, 300, 480); 
else 
    rect = CGRectMake(0, 0, 300, 480); 

[UIView animateWithDuration:0.35 animations:^{ self.view.frame = rect; }]; 
2

(我知道這是一個老問題,但我只花了一個半小時試圖找到自己的答案沒有成功,所以我想我會在這裏發佈了其他人誰卡住...特別是如果你想顯示狀態欄,你的觀點是結束了重疊的話)

我發現這個作品,如果你想隱藏狀態欄...

[[UIApplication sharedApplication] setStatusBarHidden:YES]; 
[self.view setFrame: [[UIScreen mainScreen] bounds]]; 

但不是當你想顯示狀態欄... 在這種情況下我使用這個解決方案wh ich工作,但我很擔心,因爲它將狀態欄高度硬編碼爲20 ... 它也擔心我必須根據方向以不同的方式調整視圖。但如果我沒有這樣做,它總是在錯誤的邊緣上留下20分的差距。 在我的情況下,我想關閉一些視圖的狀態欄,然後返回時返回。如果我在酒吧關閉時旋轉了設備,我遇到了特殊的問題。所以switch語句儘管很醜陋(有人可能會發佈一個更乾淨的解決方案),但它的作用。

[[UIApplication sharedApplication] setStatusBarHidden:NO]; 

CGRect frame = [[UIScreen mainScreen] bounds]; 

switch (self.interfaceOrientation) 
{ 
    case UIInterfaceOrientationPortrait: 
     frame.origin.y = 20; 
     frame.size.height -= 20; 
     break; 

    case UIInterfaceOrientationPortraitUpsideDown: 
     frame.origin.y = 0; 
     frame.size.height -= 20; 
     break; 

    case UIInterfaceOrientationLandscapeLeft: 
     frame.origin.x = 20; 
     frame.size.width -= 20; 
     break; 

    case UIInterfaceOrientationLandscapeRight: 
     frame.origin.x = 0; 
     frame.size.width -= 20; 
     break; 

} 

[self.view setFrame:frame]; 
1

如果你是因爲你不顯示狀態欄,而你Default.png加載,然後想在觀看你的第一個視圖控制器立即顯示在狀態欄有這個問題,只要確保你之前把[[UIApplication sharedApplication] setStatusBarHidden:NO];[self.window makeKeyAndVisible];在您的AppDelegate.m。它發生得非常快,你永遠不會看到啓動畫面上的狀態欄。

[[UIApplication sharedApplication] setStatusBarHidden:NO]; 
[self.window makeKeyAndVisible]; 
+0

這適用於我,只需確保在AppDelegate中的[self.window makeKeyAndVisiable]之前插入set statusBarHidden:withAnimation:方法即可。 – 2013-03-06 07:08:14

0

在搭載iOS 7,您可以使用:

setNeedsStatusBarAppearanceUpdate 

例如:

[self.mainViewController.navigationController setNeedsStatusBarAppearanceUpdate]; 

蘋果文檔:

調用此方法如果視圖控制器的狀態欄屬性,如 爲隱藏/未隱藏狀態o風格,變化。如果您在動畫塊內調用此方法 ,則動畫塊的其餘部分將與動畫一起動畫。

僅使用它的iOS 7