Reputation: 3860
So I am having extreme difficulties in understanding how I can create these same constraints that the interface builder so nicely has created for me.
I have been reading the document on apple developer site and trying to follow it, but I can't seem to get these work. I am creating a custom view controller that adds a subview and creates those same constraints for that new view when it's pushed in.(The bottom constraint is where the new view is pushed, the top view is ALWAYS the same) I have written this following code but it doesn't seem to work properly(for example when I simulate in-call status bar, the views don't act like on the initial view where IB has created the constraints)
My code:
QVViewController * __weak vc1 = (QVViewController*)self.rootViewController2.parentViewController;
UIView *viewToBePushed = tempV.view;
UIView *topContainerView = self.rootViewController1.view;
id bottomLayoutGuide = tempV.bottomLayoutGuide;
[vc1.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[viewToBePushed]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(viewToBePushed)]];
[vc1.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topContainerView]-0-[viewToBePushed]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(viewToBePushed,topContainerView)]];
[vc1.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-138-[viewToBePushed]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(viewToBePushed)]];
[vc1.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[viewToBePushed]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(viewToBePushed)]];
[vc1.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[viewToBePushed]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(viewToBePushed)]];
[vc1.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottomLayoutGuide]-0-[viewToBePushed]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(viewToBePushed, bottomLayoutGuide)]];
Basically
viewToBePushed represents the bottom container in the picture topContainerView represents the top container in the picture vc1 is the view where those two container are
I hope I explained the situation clearly enough, if not, ask and I can try to elaborate. I would be really grateful for help as these constraint things are stealing my good night sleep and I very much would want to write easily maintainable code.
So can someone show me how to correctly create those constraints in code.
Upvotes: 0
Views: 315
Reputation: 104082
Given your setup, I think the bottom container view should have whatever constraints you need, but they don't ever need to be changed. When you switch to a new controller embedded in that container view, you can just set constraints to all sides of that container (after viewThatWasPushed is added as a subview)
[self.bottomContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[viewThatWasPushed]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewThatWasPushed)]];
[self.bottomContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[viewThatWasPushed]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewThatWasPushed)]];
self.bottomContainerView is an IBOutlet to theta bottom view.
Upvotes: 1