xcode 在 InterfaceBuilder 中启用/禁用 NSLayoutConstraints

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29027867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:49:37  来源:igfitidea点击:

Enabling/Disabling NSLayoutConstraints in InterfaceBuilder

iosobjective-cxcodeinterface-builderautolayout

提问by Malte

NSLayoutConstraint(in iOS 8.0) has a BOOLproperty called activewhich makes it easy to disable/enable said layout constraint on-the-fly.

NSLayoutConstraint(in iOS 8.0) 有一个BOOL名为的属性active,可以很容易地禁用/启用所述布局约束。

To create a second layout set for a view controller which I can then programmatically enable/disable (via an IBOutletCollectionof NSLayoutConstraintsfor both sets) , I'll need to disable my already defined layout constraints in interface builder.

要为视图控制器创建第二个布局集,然后我可以以编程方式启用/禁用(通过两个集的一个IBOutletCollectionof NSLayoutConstraints),我需要在界面构建器中禁用我已经定义的布局约束。

Let me clear here I do NOT want to delete them, just disable them so that i can design a second set without interface builder complaining all the time about mismatching constraints. Furthermore switching size classes is not an option, since the layout sets are meant for one and the same size class.

让我在这里澄清一下,我不想删除它们,只需禁用它们,这样我就可以设计第二套,而无需界面构建器一直抱怨约束不匹配。此外,切换尺寸等级不是一种选择,因为布局集是为一个相同的尺寸等级而设计的。

Is there an option to do so?

有没有这样做的选择?

Thanks in Advance

提前致谢

Malte

马耳他

Further Information: SDK Version: 8.1Deployment Target 8.0

更多信息: SDK Version: 8.1Deployment Target 8.0

回答by gabbler

Select the constraint you want to disable in storyboard, and Option+Command+4to show Attributes Inspector, then unselect Installed.

选择要禁用故事板的限制,以及Option+ Command+ 4,以显示属性检查器,然后取消选择安装。

回答by Ash

I'd previously used the solution provided by Gabbler successfully, but recently I tried the same thing using Swift 2.0 and Xcode 7 and found that it no longer worked. Constraints that were set as not installed were, as one might expect, not installed at all and had no effect on the layout when switched on or off.

我之前成功地使用了 Gabbler 提供的解决方案,但最近我使用 Swift 2.0 和 Xcode 7 尝试了同样的事情,发现它不再有效。正如人们所料,设置为未安装的约束根本没有安装,并且在打开或关闭时对布局没有影响。

My solution to the problem was to ensure that all constraints were Installed and to add a user-defined runtime attribute with the key 'active', type 'boolean' and value 'false'.

我对这个问题的解决方案是确保所有约束都已安装,并添加一个用户定义的运行时属性,其键为“active”,类型为“boolean”,值为“false”。

The user-defined runtime attribute panel can be found in the Identity Inspector underneath the Custom Class fields.

用户定义的运行时属性面板可以在身份检查器中的自定义类字段下方找到。

回答by rgkobashi

My solution using Xcode 8 and Swift 3 without getting any warnings was unchecked the Installedbox on Interface builder, Inspector tab:

我使用 Xcode 8 和 Swift 3 而没有收到任何警告的解决方案在 Interface builder, Inspector 选项卡上取消选中Installed框:

Installed box on Interface builder

接口生成器上的安装框

Then create the IBOutlets and add/remove them programmatically on viewDidLayoutSubviews()

然后创建 IBOutlets 并在viewDidLayoutSubviews()上以编程方式添加/删除它们

view.removeConstraints([constraints to remove, ...])
view.addConstraints([constraints to add, ...])

Make sure to remove the constraints first, otherwise you will get the message log Unable to simultaneously satisfy constraints...

一定要先去掉约束,否则你会得到消息日志无法同时满足约束...

回答by Pomme2Poule

For this particular case, I'd declare my constraints in code.

对于这种特殊情况,我会在代码中声明我的约束。

// Card View Animatable Constraints
private lazy var cardViewHeightConstraint: NSLayoutConstraint = cardView.heightAnchor.constraint(equalToConstant: 500)
private lazy var cardViewEqualHeightConstraint: NSLayoutConstraint = cardView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.8)

You can then activate or deactivate them whenever you want.

然后,您可以随时激活或停用它们。

override func viewDidLoad() {
    super.viewDidLoad()

    cardViewHeightConstraint.isActive = true
    cardViewEqualHeightConstraint.isActive = false
}

And you still can do most of your layout in interface builder. You can set a constraint that will be removed at runtime so that Interface Builder will still reflect the appearance of your layouts (or at least one of them). Simply create a constraint and check the "Remove at build time" attribute.

而且您仍然可以在界面构建器中完成大部分布局。您可以设置将在运行时移除的约束,以便 Interface Builder 仍将反映您的布局(或至少其中之一)的外观。只需创建一个约束并检查“在构建时删除”属性。

In a constraint's size or attribute inspector, check the "Remove at build time" box.

在约束的大小或属性检查器中,选中“在构建时删除”框。

What this achieves:

这实现了什么:

  • You got to still use Interface Builder for most of your layout
  • You won't have any IB warnings
  • It works.
  • 您仍然必须在大部分布局中使用 Interface Builder
  • 您不会收到任何 IB 警告
  • 有用。

The only downside is that interface builder will not be able to fully represent your layout.

唯一的缺点是界面构建器将无法完全代表您的布局。