2012-09-22 44 views
1

從我開始使用iOS 6開始,我似乎遇到了一個問題,它在使用iOS 5時沒有出現。起初,我認爲它可能只是一個模擬器錯誤,但是由於今天在我的iPhone 5上測試了它,我可以看到它不僅僅在模擬器中。我創建一切以編程方式 - 我似乎更喜歡這樣做(我認爲這是因爲我的HTML/CSS背景!) - 但我仍然是新的Objective-C,我不能找到一個完整的例子來說明如何以編程方式設置導航控制器/表格視圖,所以我把它從我能找到的信息的金塊組合在一起,因此,我可能會做一些根本性錯誤的事情。然而,它在iOS 5上完美運行(並且仍然有效)。導航欄和表格視圖之間的黑條出現在iOS 6上

問題是我在導航欄和表視圖之間有一個黑條,但奇怪的是,如果我推視圖並返回到該原始視圖,該欄消失,並且直到我完全重新啓動應用程序纔會重新出現。

下面的圖像是在啓動應用程序(1),如我推(2)中,並且初始視圖的視圖,後我已經回到它(3):

這是我爲我的代碼:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    RootViewController *rootController = [[RootViewController alloc] init]; 
    self.window.rootViewController = rootController; 

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController]; 
    [self.window addSubview:navigationController.view]; 

    [self.window makeKeyAndVisible]; 

    NSLog(@"Window frame: %@", NSStringFromCGRect(self.window.frame)); 

    return YES; 
} 

RootViewController.m

- (void)loadView 
{ 
    self.title = @"Title"; 

    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain]; 
    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
    self.view = self.tableView; 

    NSLog(@"tableView frame: %@", NSStringFromCGRect(self.tableView.frame)); 

    UIBarButtonItem *newViewButton = [[UIBarButtonItem alloc] initWithTitle:@"New View" 
                    style:UIBarButtonItemStylePlain 
                   target:self 
                   action:@selector(newViewButtonTapped:)]; 
    [self.navigationItem setRightBarButtonItem:newViewButton animated:NO]; 
} 

我添加了NSLogs,看他們是否顯示了任何可能幫助我的東西。輸出是:

tableView frame: {{0, 0}, {320, 480}} 
Window frame: {{0, 0}, {320, 480}} 

任何人都可以給我任何想法我做錯了什麼?似乎有一個類似/相同的問題(Black bar overtop navigation bar - 在評論中),但我還沒有找到答案。

謝謝,提前!

+0

任何幫助在http://stackoverflow.com/questions/6225938/iphone-view-controller-view-shifted-down-20-pixels-when-rotating-to-landscape? –

+0

謝謝,@JereKäpyaho。我想我理解他們在某些答案中所說的話,但是我一直在處理這些代碼,不管我嘗試什麼,它都不會移動。即使我使用非常不尋常的框架,桌面視圖也不會移動。我想,黑條的大小看起來與狀態欄大小相同。 – dvyio

回答

5

您將RootViewController添加到窗口兩次,一次是通過設置UIWindow.rootViewController(內部做[window addSubview:rootViewController.view])並再次將其添加爲導航控制器的子視圖。

你應該這樣做:

RootViewController *rootController = [[RootViewController alloc] init]; 
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController]; 
self.window.rootViewController = navigationController; 

作爲一個經驗法則,從來沒有直接添加視圖到窗口,除非你知道那你真的想。

+0

非常感謝!它完美的工作! – dvyio

相關問題