gpt4 book ai didi

ios - 使用视觉格式语言更改 NSLayoutConstraints 方向的正确方法

转载 作者:可可西里 更新时间:2023-11-01 02:19:07 26 4
gpt4 key购买 nike

我有单独的 NSLayoutConstraints 数组,这些数组使用我想用于纵向和横向方向的视觉格式字符串构建。我尝试了两种在两者之间切换的方法:激活/停用它们以及添加/删除它们。

激活/停用:

portaitConstraints.forEach {
$0.active = false
}
landscapeConstraints.forEach {
$0.active = true
}

添加/删除:

self.view.removeConstraints(portraitConstraints)
self.view.addConstraints(landscapeConstraints)

这两种方法似乎都能正常工作并按预期运行,但我在使用激活/停用方法时遇到以下运行时错误:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

这有通用的模式吗?如果它的行为符合预期,我可以安全地忽略该错误吗?一种方法比另一种方法有更多的开销吗?我认为只翻转我想要激活的属性的“事件”属性比反复向 View 添加约束和从 View 中删除约束更有意义。

或者这只是一种愚蠢的方法,我需要做一些事情,比如直接更改 NSLayoutAttributes 吗?

最佳答案

你可能有一个布局歧义,一些约束可能相互冲突。如果您没有为设置约束的 View 设置 translatesAutoresizingMaskIntoConstraints = NO,通常会发生这种情况。控制台为您提供冲突约束列表,您应该解决布局歧义,因为它可能导致错误的布局。这不是致命的,但如果你想要一个好看的应用程序 - 你需要看看它。

WWDC 2015 有很棒的视频,Mysteries of Auto Layout, Part 1Mysteries of Auto Layout, Part 2描述了如何快速找到冲突的约束并解决它。这些也是从整体上理解自动布局的好视频。 WWDC 上还有一些视频,例如 WWDC2012, Auto Layout by Example , Best Practices for Mastering Auto LayoutTaking Control of Auto Layout in Xcode 5涵盖了很多关于自动布局的信息。

关于ios - 使用视觉格式语言更改 NSLayoutConstraints 方向的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com