简体   繁体   中英

Black bar between navigation bar and table view appears on iOS 6

I seem to be having an issue since I started using iOS 6, which doesn't appear when using iOS 5. At first, I thought it might just be a simulator bug, but since testing it on my iPhone 5 today, I can see that it's not just in the simulator.

I'm creating everything programmatically — I seem to prefer doing it that way (I assume it's because of my HTML/CSS background!) — but I'm still reasonably new to Objective-C, and I couldn't find a full example of how to set up a navigation controller/table view programmatically, so I put it together from the nuggets of information I could find, and therefore, I could be doing something fundamentally wrong. However, it's worked (and still works) perfectly on iOS 5.

The problem is that I have a black bar between the navigation bar and the table view, but the strange thing is that if I push a view and go back to that original view, the bar disappears, and doesn't reappear until I completely restart the app.

The following image is of the app at launch (1), as I'm pushing a view in (2), and the initial view, after I've gone back to it (3):

This is what I have as my code:

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];
}

I added the NSLogs to see if they showed anything that might help me. The output is:

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

Can anyone give me any ideas about what I'm doing wrong? It seems is having a similar/the same problem ( Black bar overtop navigation bar — in the comments), but I haven't found an answer.

Thanks, in advance!

You're adding the RootViewController to the window twice, once by setting UIWindow.rootViewController (which internally does [window addSubview:rootViewController.view] ) and again by adding it as a subview of the navigation controller.

You should be doing this:

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

As a rule of thumb, never add a view directly to the window unless you know that you actually want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM