所以我在理解如何创建接口构建器为我创建的相同约束方面遇到了极大的困难。
我一直在苹果开发者网站上阅读该文档,并尝试遵循它,但我似乎无法完成这些工作。我正在创建一个自定义视图控制器,它添加一个子视图,并在推入时为该新视图创建相同的约束。(底部约束是推送新视图的位置,顶视图总是相同的)我写了以下内容代码,但它似乎无法正常工作(例如,当我模拟调用状态栏时,视图不像IB创建约束的初始视图那样)
我的代码:
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)]];
基本上
viewToBePushed表示图片中的底部容器 topContainerView表示图片中的顶部容器 vc1是这两个容器
的视图我希望我能够清楚地解释这种情况,如果没有,请问我可以尝试详细说明。我真的很感激你的帮助,因为这些约束性东西正在偷我睡个好觉,而且我非常希望能够编写易于维护的代码。
有人可以告诉我如何在代码中正确创建这些约束。
答案 0 :(得分:1)
鉴于您的设置,我认为底部容器视图应该具有您需要的任何约束,但它们不需要更改。当您切换到嵌入在该容器视图中的新控制器时,您可以只为该容器的所有边设置约束(在viewThatWasPushed作为子视图添加之后)
[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是theta底视图的IBOutlet。