最近在使用snapkit布局时,竟然发现更新约束会导致崩溃,为什么这样呢?

        checkButton.snp.makeConstraints { (make) in
make.left.top.equalToSuperview()
make.height.equalTo(radioListSubviewButtonHeight)
make.width.equalTo(self).multipliedBy(0.5)
}
checkButton.snp.updateConstraints { (make) in
make.width.equalTo(self).multipliedBy(0.7)
}

看起来完全没有问题,但是一运行就崩溃,崩溃位置在activeIfNeeded方法中:

    internal func activateIfNeeded(updatingExisting: Bool = false) {
guard let item = self.from.layoutConstraintItem else {
print("WARNING: SnapKit failed to get from item from constraint. Activate will be a no-op.")
return
}
let layoutConstraints = self.layoutConstraints if updatingExisting {
var existingLayoutConstraints: [LayoutConstraint] = []
for constraint in item.constraints {
existingLayoutConstraints += constraint.layoutConstraints
} for layoutConstraint in layoutConstraints {
let existingLayoutConstraint = existingLayoutConstraints.first { $ == layoutConstraint }
guard let updateLayoutConstraint = existingLayoutConstraint else {
fatalError("Updated constraint could not find existing matching constraint to update: \(layoutConstraint)")
} let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute
updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute)
}
} else {
NSLayoutConstraint.activate(layoutConstraints)
item.add(constraints: [self])
}
}

fatalerror信息提示找不到因存在的constraint来更新,输出existingLayoutConstraints不为nil,输出existingLayoutConstraint却为nil,很奇怪。

点击进入first方法,发现是取第一个满足谓词条件的值,而不是简单的取第一个值:

    /// Returns the first element of the sequence that satisfies the given
/// predicate.
///
/// The following example uses the `first(where:)` method to find the first
/// negative number in an array of integers:
///
/// let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
/// if let firstNegative = numbers.first(where: { $0 < 0 }) {
/// print("The first negative number is \(firstNegative).")
/// }
/// // Prints "The first negative number is -2."
///
/// - Parameter predicate: A closure that takes an element of the sequence as
/// its argument and returns a Boolean value indicating whether the
/// element is a match.
/// - Returns: The first element of the sequence that satisfies `predicate`,
/// or `nil` if there is no element that satisfies `predicate`.
///
/// - Complexity: O(*n*), where *n* is the length of the sequence.
public func first(where predicate: (Element) throws -> Bool) rethrows -> Element?

在此处谓词条件为 $0 == layoutConstraint ,进入LayoutConstraint类中,发现==运算符已被重载:

internal func ==(lhs: LayoutConstraint, rhs: LayoutConstraint) -> Bool {
guard lhs.firstItem === rhs.firstItem &&
lhs.secondItem === rhs.secondItem &&
lhs.firstAttribute == rhs.firstAttribute &&
lhs.secondAttribute == rhs.secondAttribute &&
lhs.relation == rhs.relation &&
lhs.priority == rhs.priority &&
lhs.multiplier == rhs.multiplier else {
return false
}
return true
}

在判断相等时,竟然还判断了multiplier,本例中更新约束前后multiplier不相等,所以找不到对应的约束。

解决方法:

要想修改multiplier,不能使用update,要使用remake

snapkit issue中相似问题

snapkit更新约束崩溃的问题的更多相关文章

  1. Xcode8更新约束

    Xcode升级之后就会发现约束设置好,想更新一下约束,看看约束是不是刚刚好,习惯性的去点右下角的更新约束的结果却发现没有更新约束的这一项了,好尴尬. 后来发现原来在Xcode8的约束更新换了一个地方, ...

  2. Masonry整体动画更新约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  3. Masonry remake更新约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  4. Masonry 动画更新约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  5. Android app主线程UI更新间歇性崩溃的问题

    对App进行开发测试时,偶尔出现app崩溃的问题.日志如下: 10-25 18:44:52.935 15290-15290/com.zzq.cnblogs E/AndroidRuntime﹕ FATA ...

  6. iOS:在tableView中通过Masonry使用autolayout在iOS7系统出现约束崩溃

    一.出现崩溃情景: 给tableView创建一个头视图,也即tableHeaderView,然后使用Masonry并切换到iOS7/7.1系统给tableHeaderView中的所有子视图添加约束,此 ...

  7. SnapKit代码约束

    let label = UILabel() label.frame = CGRectMake(, , , ) label.backgroundColor = UIColor.cyanColor() l ...

  8. Masonry约束崩溃

    报错: Trapped uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint const ...

  9. ios 更新约束

    [view setNeedsUpdateConstraints];    [view updateConstraintsIfNeeded];    [view setNeedsLayout];    ...

随机推荐

  1. opencv3.2.0图像处理之中值滤波medianBlur API函数

    /*中值滤波:medianBlur函数是非线性滤波 函数原型:void medianBlur(inputArray src,OutputArray dst,int ksize) 参数详解: input ...

  2. TileStache生成切片

    1.tilestache.cfg { "cache": { "name": "Disk", "path": " ...

  3. 最长斐波那契序列-LeetCode-873

    英文版A sequence X_1, X_2, ..., X_n is fibonacci-like if: - n >= 3- X_i + X_{i+1} = X_{i+2} for all ...

  4. vip会员统计表 (vip等级是灵活配置的 非写死1是金卡用户 2是什么 等)

      一个非常常见的报表,分析会员组成比例 以及最新增长情况 和上月同期会员增长情况. 比较特殊一点的是 报表中的 普通会员 和 金卡会员 临时会员 银卡会员 等列 都是根据会员等级配置表动态生成的(即 ...

  5. 报表在IBM AIX系统下resin部署

     报表是用java开发的,具有良好的跨平台性.不仅可以应用在windows.linux.操作系统,还可以应用在AIX等等的unix操作系统.在各种操作系统上部署过程有一些差别.下面说一下在AIX操 ...

  6. 前端构建工具 Gulp.js 上手实例

    在软件开发中使用自动化构建工具的好处是显而易见的.通过工具自动化运行大量单调乏味.重复性的任务,比如图像压缩.文件合并.代码压缩.单元测试等等,可以为开发者节约大量的时间,使我们能够专注于真正重要的. ...

  7. 实验二:klee处理未建模函数和处理error的方式

    首先,能够分析klee源码固然重要.但是目前尚未到那个地步.我按照我的过程,记录和分析我所做的实验. 结论性内容是: 1.klee处理printf传入符号值的情形时,报为error,不会将符号值具体化 ...

  8. UINavigationController便于pop的category

    UINavigationController便于pop的category 效果图: 这个category是为了方便UINavigationController用于跳转到指定的控制器当中,用于跳级,如果 ...

  9. Linux 系统的日志目录

    连接时间的日志 连接时间日志一般由/var/log/wtmp和/var/run/utmp这两个文件记录,通过who查看 who /var/log/wtmp [连接时间日志] who /var/log/ ...

  10. esxcli software vib 命令为 ESXi 5.x/6.x 主机安装补丁程序 (2008939)

      参考KB:https://kb.vmware.com/s/article/2008939?lang=zh_CN    Symptoms 免责声明:本文为 “esxcli software vib” ...