【问题标题】:Constraint change is not working约束更改不起作用
【发布时间】:2014-08-12 06:37:22
【问题描述】:

我有一个观点,其约束如下。

现在,当我按照以下方式更改约束时:

     [cell addConstraint:[NSLayoutConstraint constraintWithItem:cell.view_label 
    attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual 
    toItem:cell.view_label.superview 
attribute:NSLayoutAttributeLeading multiplier:1.0 constant:60]];

            [UIView animateWithDuration:0.2 animations:^{
                [cell.view_label updateConstraints];
                [cell.view_label.superview updateConstraints];
            }];

//不工作

写:

试试这个:(1)查看每个约束并尝试找出你的 不要期待; (2) 找到添加了不需要的约束的代码或 约束并修复它。 (注:如果你看到 NSAutoresizingMaskLayoutConstraints 你不明白的,参考 到 UIView 属性的文档 translatesAutoresizingMaskIntoConstraints) ( "", "")

将尝试通过打破约束来恢复

中断 objc_exception_throw 以在调试器中捕获它。这 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法 中列出的也可能有帮助。

帮我解决这个问题

提前致谢

【问题讨论】:

  • 更改约束的优先级,1000 的优先级使其成为必需的约束。
  • @tdelepine,感谢您的回答!但我已经尝试过所有优先事项。
  • @user2893370 在 IB 中为约束添加一个出口,并在代码中更改其常量值。
  • @Akhilrajtr,感谢您的回答!它的工作......

标签: ios objective-c uiview autolayout


【解决方案1】:

您正在那里添加一个 new 约束。旧的约束仍然存在,它不能同时满足两者,因此发出警告。

您需要修改现有约束的constant 属性。做到这一点的最好方法是为它提供一个出口。

【讨论】:

    【解决方案2】:

    试试这个,

    由于您在 IB 中已经有一个约束,请在代码中更新它。您无需出于相同目的添加约束。将IBOutlet 连接到该约束并在代码中修改其constant 值。

    【讨论】:

    • 我接受了你的回答。
    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2018-11-30
    • 2018-12-30
    • 2015-03-13
    • 2017-08-24
    • 2014-11-20
    相关资源
    最近更新 更多