UINavigationController initWithRootViewController,视图没有出现

时间:2012-07-31 14:32:48

标签: ios ipad uinavigationcontroller uitableview

我正在尝试使用基于导航的功能来显示不同的表视图而没有运气。基本上用于initWithRootViewController的视图未正确显示,但导航栏是。以下是viewDidLoad TimerViewController方法中具有层次结构AppDelegate - >的代码ViewController - > TimerViewController

incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [incidentTableViewController.tableView.backgroundView setBackgroundColor:[UIColor colorWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    [incidentTableViewController.view setFrame:CGRectMake(0, 0, 268, 423)];
    [incidentTableViewController.tableView showsVerticalScrollIndicator];
    [incidentTableViewController setTitle:@"Incidents"];
    [incidentTableViewController.navigationController setNavigationBarHidden:NO];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];
    [controller.view setFrame:CGRectMake(268, 0, 268, 423)];
    [controller.view setBackgroundColor:[UIColor clearColor]];
    [controller.navigationController setNavigationBarHidden:YES];
    //[controller.view addSubview:incidentTableViewController.view];
    [self.view addSubview:controller.view];

这导致(我不确定为什么导航栏上方也存在间隙):

enter image description here

如果我从最后一行[controller.view addSubview:incidentTableViewController.view];取消注释第二行,我得到的结果是所需的减去导航栏:

enter image description here

我想要实现的是第二张图片带有导航栏,任何想法?

4 个答案:

答案 0 :(得分:0)

你为什么要这样做?

[controller.navigationController setNavigationBarHidden:YES];

顺便说一句,在appDeidfinunching你的appDelegate执行此操作:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

[incidentTableViewController.tableView.backgroundView setBackgroundColor:[UIColor colorWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
[incidentTableViewController.view setFrame:CGRectMake(0, 0, 268, 423)];
[incidentTableViewController.tableView showsVerticalScrollIndicator];
[incidentTableViewController setTitle:@"Incidents"];
[incidentTableViewController.navigationController setNavigationBarHidden:NO];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];
[controller.view setFrame:CGRectMake(268, 0, 268, 423)];
[controller.view setBackgroundColor:[UIColor clearColor]];
self.window.rootViewController = incidentTableViewController;

[self.window makeKeyAndVisible];

如果您的窗口有一个rootViewcontroller,那么您应该将incidentTableViewController推送到导航堆栈或以模态方式显示incidentTableViewController

答案 1 :(得分:0)

您必须更改项目的逻辑。您可以启动一个新的选项卡项目并修改didFinishLaunchingWithOptions方法 这里我展示了带有2个选项卡的示例,一个带有示例视图的选项卡,另一个带有工具栏:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //view controller for 1st tab
    UIViewController * viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];

    //your view controller with bar for the 2d tab
    IncidentTableViewController *incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [incidentTableViewController.tableView.backgroundView setBackgroundColor:[UIColor colorWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    [incidentTableViewController.tableView showsVerticalScrollIndicator];
    [incidentTableViewController setTitle:@"Incidents"];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, controller, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

答案 2 :(得分:0)

UINavigationController没有自定义视图。

 [self.view addSubview:controller.view];

--->

 [self presentModalViewController:controller animated:YES];

[self presentViewController:controller animated:YES completion:^{}];

答案 3 :(得分:0)

使用导航控制器初始化的UISplitViewController作为其视图控制器修复了问题,工作代码如下:

incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];
    incidentTableViewController.title = @"Incidents";
    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.viewControllers = [NSArray arrayWithObjects:controller, nil];
    splitViewController.delegate = (id)incidentTableViewController;
    splitViewController.view.frame = CGRectMake(268, 0, 268, 423);
    [self.view addSubview:splitViewController.view];

enter image description here