导航栏和表格视图之间的黑条显示在iOS 6上

时间:2012-09-22 20:44:34

标签: objective-c ios uitableview uinavigationcontroller ios6

自从我开始使用iOS 6后,我似乎遇到了一个问题,这在使用iOS 5时没有出现。起初,我认为它可能只是一个模拟器错误,但自从今天在我的iPhone 5上测试它,我可以看到它不只是在模拟器中。

我正在以编程方式创建所有内容 - 我似乎更喜欢这样做(我认为这是因为我的HTML / CSS背景!) - 但我仍然是Objective-C的新手,我找不到一个完整的例子,说明如何以编程方式设置导航控制器/表视图,所以我把它放在我能找到的信息块中,因此,我可能会做一些根本错误的事情。但是,它在iOS 5上完美运行(并且仍然有效)。

问题是我在导航栏和表格视图之间有一个黑条,但奇怪的是,如果我按下一个视图并返回原始视图,该条消失,并且直到我完全重启了应用程序。

以下图片是启动时的应用程序(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 - 但是我没有找到答案。

先谢谢!

1 个答案:

答案 0 :(得分:5)

您将RootViewController添加到窗口两次,一次是通过设置UIWindow.rootViewController(内部执行[window addSubview:rootViewController.view])并再次将其添加为导航控制器的子视图。

你应该这样做:

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

根据经验,除非您知道您实际想要的,否则永远不要将视图直接添加到窗口。