2012-01-15 34 views
9

設置時看起來很簡單,但我無法解釋爲什麼狀態欄和導航欄之間存在這種差距。另外,包含的視圖看起來好像可以正確對齊,並且它只是向下移動的導航欄。這個差距看起來像狀態欄的大小,所以我期望這與它有關,但我不知道是什麼。UINavigationController頂部有額外的狀態欄間隙

Nav Bar with extra space

這裏是用於設置導航控制器的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    advancedVC = [[AdvancedSearchFormVC alloc] initWithNibName:@"AdvancedSearchForm" bundle:nil]; 
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:advancedVC]; 
    nav.navigationBar.tintColor = [UIColor defaultNavBarTint]; 
    nav.navigationBar.topItem.title = NSLocalizedString(@"SearchTitle", nil); 
    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"SearchButton", nil) style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];   
    nav.navigationBar.topItem.rightBarButtonItem = searchButton; 

    self.view = nav.view; 
} 

的RootViewController的使用從XIB文件,其中我有模擬狀態欄,導航條的視圖,並且標籤欄。

+0

請注意,這是iOS 5和更早版本之間存在差異的區域。如果你使用導航控制器的「canned」佈局,它是相當便攜的,但是如果你已經定製了它,那麼這兩個版本之間的事情會有很大的變化。 (我從來沒有完全明白造成這種差異的原因,但是您展示的是iOS 5上運行的iOS 4代碼的典型代碼。) – 2012-01-15 22:25:04

+0

請參閱此問題的第一個答案http://stackoverflow.com/questions/5850705/why-do-navigation-appear-20-pixels-below-status-bar-in-the-view – CarmeloS 2013-04-03 06:52:57

回答

0

通過修復導航控制器的插入方式解決了問題。導航控制器應該直接放置在導航控制器上,而不是將其插入到已經放置在標籤欄控制器上的視圖中。

advancedSearchFormVC = [[AdvancedSearchFormVC alloc] initWithNibName:@"AdvancedSearchForm" bundle:nil]; 
UINavigationController *searchNavController = [[UINavigationController alloc] initWithRootViewController:advancedSearchFormVC]; 

這只是一個控制器,在tabbar控制器上,同時替換advancedSearchFormVC。然後,這個導航控制器被添加到控制器的數組,並將其放到tabbar控制器上。

對不起,有任何問題,但這是我可以直接看,而不是看到它的問題之一。我之前應該看到過這種情況,因爲我已經在TabBar控制器上安裝了另一個導航控制器,並且它的設置方式與此相同。

感謝您的協助。

+2

Jim - 我沒有完全按照你的解決方案。你可以發佈代碼來顯示實際修復嗎? – DoctorG 2012-06-29 09:36:01

0

既然你加入advancedVC爲的self.view一個子視圖,它被的self.view我猜已經補償了狀態欄框內添加。

你或許可以很容易地加入這一行解決這個問題:

nav.view.frame = self.view.frame; 

只是上面這條線:

self.view = nav.view; 

-

其他的想法

我不知道你的整個設置,但可能根本不需要。只需使您的App Delegate中包含的UIWindow實例的advancedVC實例爲rootViewController即可。

7

我以前有同樣的問題。我曾經UINavigationBar的增加的UIViewController代碼:

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self]; 
[self.view addSubview:nc.view]; 

解決方案:
勾選「想全屏」與您的UIViewController屬性檢查器。

10

問題的確是,導航控制器總是期望爲狀態欄留下空間,這是20像素的差距。我搜索了一段時間才找到這個解決方案,它的工作原理:

//nav is assumed to be a subclass or instance of UINavigationController 
nav.view.frame = CGRectOffset(nav.view.frame, 0.0, -20.0); 
//you can then add the navigation's view as a subview to something else 

我最初發現一個答案,這樣做偏移到導航欄的看法,但沒有奏效。它在您對導航控制器實際視圖執行操作時起作用。

我使用這種技術將導航控制器從另一個筆尖添加到我的主筆尖的空視圖,因此我可以將它定位在主屏幕中的任何位置作爲子視圖。通過在我的主要筆尖上使用空視圖作爲佔位符和定位框架,我創建了一個單獨的筆尖和類來管理導航,該導航管理用於處理其屏幕的其他筆尖。這樣,我可以解決經典的「我如何在導航控制器上添加橫幅,圖像或自定義視圖」,同時將導航控制器作爲子視圖...在iOS 5中具體說明。

另外值得一提的是,我使用應用程序委託來存儲和訪問所有其他控制器,因此它們被持久實例保留,我可以從任何類訪問它。在所有控制器的應用程序委託中創建併合成一些屬性,然後在viewDidLoad創建實例中進行合成。這樣,我可以通過添加引用我的應用程序後的任何地方使用的所有控制器:

//this shows how to store your navigation controllers in the app delegate 
//assumes you've added 2 properties (UINavigationController*)"navController" and (UIViewController*)"rootController" in your app delegate 
//...don't forget to add #import "AppDelegate.h" to the top of the file 

AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
[app.navController pushViewController: app.rootController animated:YES]; 
//now apply the offset trick to remove the status gap 
app.navController.view.frame = CGRectOffset(app.navController.view.frame, 0.0, -20.0); 
+0

已找到相同的解決方案,但我不滿意它... – iWheelBuy 2012-12-24 09:56:28

+0

@Ali謝謝你的解決方案!非常好! – gaussblurinc 2013-08-05 13:14:47

1

大家都知道,現在,20個像素位移爲頂部的狀態欄提供了空間。 但實際上,視圖控制器座標系保持不變,只有導航欄框向下移動20個像素。這使得導航欄實際上重疊了視圖的前20個像素。

記錄導航欄幀起源,它會顯示(0.0,20.0)

因此該解決方案是在viewWillAppear中簡單地重新定位導航欄的原點至(0.0,0.0)。

self.navigationController.navigationBar.frame = CGRectMake(0.0, 0.0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height); 
7

你可以嘗試設置屬性下最熱門酒吧選中屬性的UIViewController部分。

+0

這對我有用,謝謝。 – 2014-07-17 05:39:12

0

問題是應該將UINavigationController.view添加到頂視圖。 只要找到最頂級的一個,它會工作得很好。

相關問題