Search code examples
objective-cios6uinavigationcontrollerautolayout

UINavigationController & Auto-Layout: RootViewController's view is not shown


I'm trying to use a navigation controller in my iOS-Project. Here is the setup of my AppDelegate class:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

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

    // Generate RootViewController (some auto-layout stuff is going on here)
    MyLayoutDescriptionGeneratorClass *generator = [MyLayoutDescriptionGeneratorClass new];
    MyViewControllerClass *vc = [generator generateViewController];

    self.navController = [[UINavigationController alloc] initWithRootViewController:vc];
    [self.window setRootViewController:self.navController];
    [self.window makeKeyAndVisible];
    return YES;
}

The viewDidLoad of my view controller looks like this:

-(void)viewDidLoad{

    [super viewDidLoad];
    // Generate all subviews and their constraints        
    self.view = [MyObjectManagerClass viewForParentViewDescription:_parentViewDescription];
    self.title = @"Hello";
}

When I Build & Run the navigation bar is shown and its title is set to Hello - unfortunately the view controllers view is not displayed.

If I take out the navigation controller like this

[self.window setRootViewController:self.navController];

everything works great. Can anyone help me please?


Solution

  • I just had to modify my viewDidLoad:

    -(void)viewDidLoad{
        [super viewDidLoad];
        // Generate all subviews and their constraints        
        UIView *view = [RuiObjectManager viewForParentViewDescription:_parentViewDescription];
        self.title = @"Hello";
    
        // Add generated view to self.view and create constraints
        [self.view addSubview:view];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(@"view", view)]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(@"view", view)]];
    }