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.
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):
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):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With