Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewController in UINavigationController not filling view

I have not found how to make my view fill the available space as the root view controller (or pushed view controller) in a UINavigationController. I am attempting to use AutoLayout here, but I doubt that's the issue. In my app delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ViewController *viewController = [[ViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = navigationController;

    [self.window makeKeyAndVisible];

    return YES;
}

And in my ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    // Do any additional setup after loading the view, typically from a nib.

    UILabel *companyLabel = [[UILabel alloc] init];
    companyLabel.translatesAutoresizingMaskIntoConstraints = NO;
    companyLabel.text = @"We Build It";
    companyLabel.backgroundColor = [UIColor redColor];

    [self.view addSubview:companyLabel];

    NSDictionary *views = NSDictionaryOfVariableBindings(companyLabel);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[companyLabel]" options:0 metrics:@{} views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[companyLabel]" options:0 metrics:@{} views:views]];
}

And the label is centered in the screen instead of bound to the upper left because, I believe, that the view is just centered in the screen instead of filling the available space.

like image 754
Travis Avatar asked Jun 28 '13 19:06

Travis


1 Answers

Try to remove the following line:

self.view.translatesAutoresizingMaskIntoConstraints = NO;

This will let the view controller view to have its full width and height according to the default autoresizing masks it has. This is what I get (I set the background color of the view to be green):

enter image description here

If the line above is still added, this is what I get (which seems to imply the view is not found anywhere since there is no trace of green color):

enter image description here

like image 105
Valent Richie Avatar answered Nov 20 '22 22:11

Valent Richie