0
votes

I assigned the root view controller of a navigation controller as a view with a tableview using autolayout contraints to fill the whole screen. Here is how I am setting the navigation controller as my root view controller and the root view controller in the navigation controller:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    TestViewController *vc = [[TestViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
    [self.window setRootViewController:navController];
    [self.window makeKeyAndVisible];
    return YES;
}

When the view controller loads I know it has because it calls the tableview delegate methods and data source methods as follows:

- (void)setup {
    items = @[@1014, @2022, @3001, @4212, @5323, @6345, @7111, @8010, @9040, @10500];
    viewTableView = [UITableView new];
    viewTableView.delegate = self;
    viewTableView.dataSource = self;
    [viewTableView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:viewTableView];
    self.title = @"overlay test";
}

Navigation bar title is populated with "Overlay test" successfully.

- (void)setControllerConstraints {
    NSDictionary *viewsDictionary = @{@"tableView":viewTableView};
    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[tableView]-0-|" options:0 metrics:nil views:viewsDictionary];
    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tableView]-0-|" options:0 metrics:nil views:viewsDictionary];
    [self.view addConstraints:horizontalConstraints];
    [self.view addConstraints:verticalConstraints];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setup];
    [self setControllerConstraints];
    NSLog(@"TestViewController did load");
}

I can see that the NSLog at the end of viewdidload is being printed.

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource Delegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"count: %i", [items count]);
    return [items count];
}

Item count is returning as 10.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"exampleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    [cell.textLabel setText:[[items objectAtIndex:indexPath.row] stringValue]];

    return cell;
}

However I do not see anything in the viewcontrollers view. If I set the window rootviewcontroller to 'vc' in my app delegate the tableview shows itself and it is populated. if I set the rootviewcontroller to 'navigationController' I do not see the tableview at all.

What is the problem? What am I missing/forgetting to add?

1
Nope it does not use a xib file or storyboard and uses NSLayoutConstraints to set the frame size to the width and height of the screen. the methods I have linked 'viewdidload', 'setup' and 'setControllerConstraints' hopefully reflect this.jimbob
NSLog the frame of the view of the view controller in viewDidAppear.duci9y
The frame is getting set to 0 for width and height cant figure out why though.jimbob

1 Answers

1
votes

The problem is this line,

[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

You shouldn't set that property to NO for the controller's main view. I'm not sure why it works when the controller is not in a navigation controller, but I get the weird result that the controller's viewDidAppear method is not called with that line present (when the controller is not embedded in a navigation controller).