如何在目标c中启用自动布局时增加uiview的高度

时间:2016-01-08 08:35:53

标签: ios objective-c uiview autolayout

我尝试使用下面的代码以编程方式在运行时关闭接口构建器中的特定子视图的自动布局。

[view removeConstraints:view.constraints];

有人建议这样做:

  

如果您需要更改自动布局处于活动状态的视图的高度,则需要为高度约束创建一个IBOutlet并在运行时修改它,即:

@IBOutlet weak var heightConstraint: NSLayoutConstraint!
self.heightConstraint.constant = 200

但我不明白这意味着什么。

5 个答案:

答案 0 :(得分:5)

你必须添加:

[self layoutIfNeeded];
更改高度限制后,

到您的代码。它将刷新视图和约束。 它只是意味着您可以将其保留在视图中并创建约束以动态更改高度,而不是删除自动布局。

您可以按照此tutorial了解自动布局如何工作

答案 1 :(得分:1)

请阅读并理解Apple有关自动布局的文档。

目前,如果删除所有约束,则不会禁用自动布局。

您需要在代码中引用高度约束并按照建议进行修改,然后按照其他答案的建议进行[view layoutIfNeeded]修改。

如果还没有高度限制,您也可以在运行时通过addConstraints:添加一个新约束。

您能否详细说明您缺少什么,以遵循您的引用建议?

修改: 另请注意,您引用的建议是快速的,而不是Obj-C,因此复制&粘贴不会起作用。

编辑2

我们一个接一个地走。

@IBOutlet weak var heightConstraint: NSLayoutConstraint!
  • @IBOutlet:从接口构建器注入以下变量声明的值。通过拖放连接起来。
  • weak:Obj-C和Swift中的关键字。引用计数不会通过赋值或使用此变量来增加或管理。 (参见'保留周期'和'记忆管理'。)
  • var:此声明的值可在初始化后重新分配。与let相反;在Swift中,变量可以声明为不可变的。
  • heightConstraint:声明变量的名称
  • ::读作“类型”
  • NSLayoutConstraint!:声明的变量的类型。在这种情况下,隐式展开的NSLayoutConstraint可选项。这大致转换为Obj-C类型NSLayoutConstraint*

现在,

self.heightConstraint.constant = 200

在适合你的时候在代码中的某处设置上面声明的变量的常量。在此示例中为200

答案 2 :(得分:0)

您需要在设置约束值后添加。

[self layoutIfNeeded];

答案 3 :(得分:0)

@IBOutlet weak var heightConstraint: NSLayoutConstraint! 
self.heightConstraint.constant = 200

常数: 常量添加到参与约束的乘法第二个属性。

与其他属性不同,可以在创建约束后修改常量。

在现有约束上设置常量比删除约束并添加一个与旧的完全一样的新常量要好得多,只是它具有不同的常量。

答案 4 :(得分:0)

在得到格雷戈里和扬的帮助之后,我现在能够逐步解释它。

1)将uiView的高度约束拖到.h文件中。

2)然后用名字引用它,在我的例子中我使用heightConstraint。

3)使用以下代码来实现这一目标。

1>C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\Microsoft.NetNative.targets(669,5): error MSB4062: The "SharedAssemblyApplicabilityVerifierTask" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\x64\ilc\ilc.exe. Could not load file or assembly 'file:///C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\x64\ilc\ilc.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\Users\ztl87\Documents\Visual Studio 2015\Projects\UnihanHelper\UnihanHelper\platforms\windows\CordovaApp.Windows10.jsproj]
1>C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\Microsoft.NetNative.targets(405,5): error MSB4062: The "ComputeManagedBinaries" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\x64\ilc\ilc.exe. Could not load file or assembly 'file:///C:\Program Files (x86)\MSBuild\Microsoft\.NetNative\x64\ilc\ilc.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\Users\ztl87\Documents\Visual Studio 2015\Projects\UnihanHelper\UnihanHelper\platforms\windows\CordovaApp.Windows10.jsproj]
1>MDAVSCLI : error : Error code 1 for command: C:\Program Files (x86)\MSBuild\14.0\bin\amd64\msbuild with args: C:\Users\ztl87\Documents\Visual Studio 2015\Projects\UnihanHelper\UnihanHelper\platforms\windows\CordovaApp.Windows10.jsproj,/clp:NoSummary;NoItemAndPropertyList;Verbosity=minimal,/nologo,/p:Configuration=release,/p:Platform=x64
1>  Command finished with error code 2: cmd /s /c ""C:\Users\ztl87\Documents\Visual Studio 2015\Projects\UnihanHelper\UnihanHelper\platforms\windows\cordova\build.bat" --release "--buildConfig=C:\Users\ztl87\Documents\Visual Studio 2015\Projects\UnihanHelper\UnihanHelper\build.json" --archs=x64 --win"
1>ERROR building one of the platforms : error : cmd: Command failed with exit code 2
1>  You may not have the required environment or OS to build this project
1>MDAVSCLI : error : cmd: Command failed with exit code 2