【问题标题】:AutoLayout - adding constraints programatically crashes appAutoLayout - 以编程方式添加约束使应用程序崩溃
【发布时间】:2014-01-05 13:44:18
【问题描述】:

我遇到了 AutoLayout 的问题,这让我非常头疼。我使用 Autolayout 在 .xib 中设置了滚动视图和容器视图。我需要动态地将子视图添加到容器视图中,并试图让它们与自动布局一起玩得很好。这不顺利...

这是我要完成的工作的图片:

所以 Scroll View 和 Container Layer 已经在 xib 中设置了自动布局和视图 1-3 我正在尝试以编程方式添加。我的第一个方法是:

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[viewOne]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewOne)]]

导致:

无法同时满足约束。 以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

(
      "<NSLayoutConstraint:0x9d84de0 H:|-(20)-[UIView:0x9d922e0]   (Names: '|':UIView:0x9d91fb0 )>",
      "<NSAutoresizingMaskLayoutConstraint:0x9d9fe90 h=--& v=--& UIView:0x9d922e0.midX ==>"
)

我也尝试过使用constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant,但我很难让它以我想要的方式工作。我做错了什么?

斯蒂芬

【问题讨论】:

  • 如何创建 viewOne?你在 viewOne 上做 translatesAutoresizingMaskIntoConstraints = NO 吗?
  • 您能否向我们提供有关您的子视图的更多信息?
  • viewOne 是使用[UIView alloc] init] 创建的。我也试过[UIView alloc] initWithFrame:CGRectNull]
  • 子视图最终会变得更加复杂,但现在它们只是空视图。我还没有尝试过translatesAutoResizingMaskIntoConstraints,但现在会尝试,ty

标签: ios objective-c cocoa-touch autolayout


【解决方案1】:

另一个可能的原因:使用乘数 0 而不是 1...

【讨论】:

  • 有时是我们意想不到的事情——比如这个!
【解决方案2】:
  1. 确保所有子视图的 translatesAutoresizingMaskIntoConstraints 为 NO。
  2. 确保不要为您的子视图设置框架,因为 AutoLayout 谈论的是视图的关系而不是其框架。
  3. 确保您有足够的约束条件使系统不会混淆。

【讨论】:

  • 谢谢。主要问题是translatesAutoResizingMaskIntoConstraints 以及在编写我的视觉格式语言时有点粗心。
【解决方案3】:

我遇到了完全相同的问题(令人沮丧,但调试器提供了一个很好的提示。)在我的情况下,我在将子视图添加到超级视图之前创建了一个超级视图约束。看起来这应该可行,因为没有实际的超级视图对象传递给约束构建器函数。这是之前和之后(我的速度很快,希望你能跟上):

破解版:

let v0 = UILabel(frame: CGRect(x: 100, y: 50, width: 200, height: 20))
v0.text = "Hello"
let v1 = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 20))
v1.text = "World"


let c0 : AnyObject[]! = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:|-20-[top]-10-[bottom]",
    options: NSLayoutFormatOptions.fromMask(0),
    metrics: nil,
    views: ["top":v0, "bottom":v1])

mainView.autoresizesSubviews = true

for v in [v0, v1, mainView]
{
    mainView.setTranslatesAutoresizingMaskIntoConstraints(false)
}

for v in [v0, v1]
{
    mainView.addSubview(v)
}


mainView.addConstraints(c0)

固定版本:

let v0 = UILabel(frame: CGRect(x: 100, y: 50, width: 200, height: 20))
v0.text = "Hello"
let v1 = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 20))
v1.text = "World"

mainView.autoresizesSubviews = true

for v in [v0, v1, mainView]
{
    mainView.setTranslatesAutoresizingMaskIntoConstraints(false)
}

for v in [v0, v1]
{
    mainView.addSubview(v)
}


let c0 : AnyObject[]! = NSLayoutConstraint.constraintsWithVisualFormat(
    "V:|-20-[top]-10-[bottom]",
    options: NSLayoutFormatOptions.fromMask(0),
    metrics: nil,
    views: ["top":v0, "bottom":v1])

mainView.addConstraints(c0)

【讨论】:

  • 欢迎来到 StackOverflow!虽然这可能从基本意义上回答了这个问题,但您能否详细说明为什么它有效?这可以说是一个仅代码的答案(因为代码本身没有解释),这是不鼓励的。
猜你喜欢
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
相关资源
最近更新 更多