ViewController in a UINavigationController not populating the view

I did not find how to make my view fill the available space as the root view controller (or the pressed view controller) in the UINavigationController . I am trying to use AutoLayout here, but I doubt the problem. In my application 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 shortcut is centered on the screen instead of snapping to the upper left corner, because, in my opinion, the view is simply focused on the screen instead of filling the available space.

+6
source share
1 answer

Try deleting the following line:

 self.view.translatesAutoresizingMaskIntoConstraints = NO; 

This will allow the view controller view to have full width and height according to the default startup masks. This is what I get (I set the background color in the view to green):

enter image description here

If the line above is added, this is what I get (which apparently means the view was not found anywhere, since there is no trace of green):

enter image description here

+8
source

Source: https://habr.com/ru/post/948347/


All Articles