导航栏和表格视图之间的黑条显示在iOS 6上 [英] Black bar between navigation bar and table view appears on iOS 6
问题描述
自从我开始使用iOS 6后,我似乎遇到了一个问题,这在使用iOS 5时没有出现。首先,我认为它可能只是一个模拟器错误,但自从今天在我的iPhone 5上测试它,我可以看到它不只是在模拟器中。
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.
我正在以编程方式创建所有内容 - 我似乎更喜欢这样做(我认为这是因为我的HTML / CSS背景!) - 但我仍然是Objective-C的新手,我找不到如何以编程方式设置导航控制器/表视图的完整示例,所以我把它放在一起从信息块中我可以找到,因此,我可能会做一些根本错误的事情。但是,它在iOS 5上完美运行(并且仍能正常工作)。
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.
下图是启动时的应用程序(1),因为我正在推动(2)中的视图,以及我回到它之后的初始视图(3):
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):
这就是我的代码:
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以查看它们是否显示了可能对我有帮助的任何内容。输出为:
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.
提前谢谢!
推荐答案
您将RootViewController添加到窗口两次,一次是通过设置UIWindow.rootViewController(内部执行 [window addSubview:rootViewController.view]
)并再次将其添加为导航控制器的子视图。
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.
你应该这样做:
RootViewController *rootController = [[RootViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window.rootViewController = navigationController;
根据经验,除非知道<,否则永远不要将视图直接添加到窗口/ em>你真的想要。
As a rule of thumb, never add a view directly to the window unless you know that you actually want to.
这篇关于导航栏和表格视图之间的黑条显示在iOS 6上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!