snapkit更新约束崩溃的问题
最近在使用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更新约束崩溃的问题的更多相关文章
- Xcode8更新约束
Xcode升级之后就会发现约束设置好,想更新一下约束,看看约束是不是刚刚好,习惯性的去点右下角的更新约束的结果却发现没有更新约束的这一项了,好尴尬. 后来发现原来在Xcode8的约束更新换了一个地方, ...
- Masonry整体动画更新约束
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- Masonry remake更新约束
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- Masonry 动画更新约束
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- Android app主线程UI更新间歇性崩溃的问题
对App进行开发测试时,偶尔出现app崩溃的问题.日志如下: 10-25 18:44:52.935 15290-15290/com.zzq.cnblogs E/AndroidRuntime﹕ FATA ...
- iOS:在tableView中通过Masonry使用autolayout在iOS7系统出现约束崩溃
一.出现崩溃情景: 给tableView创建一个头视图,也即tableHeaderView,然后使用Masonry并切换到iOS7/7.1系统给tableHeaderView中的所有子视图添加约束,此 ...
- SnapKit代码约束
let label = UILabel() label.frame = CGRectMake(, , , ) label.backgroundColor = UIColor.cyanColor() l ...
- Masonry约束崩溃
报错: Trapped uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint const ...
- ios 更新约束
[view setNeedsUpdateConstraints]; [view updateConstraintsIfNeeded]; [view setNeedsLayout]; ...
随机推荐
- Pig foreach用法举例
foreach:一行一行的遍历数据,处理一行的数据,然后返回一个tuple. users = load '/users.data'; 1)别名引用 f = foreach users genera ...
- with admin option /with grant option
1. with admin option是用在系统权限上的,with grant option是用在对象权限上的. SQL> grant create synonym to scott with ...
- 团队项目个人进展——Day05
一.昨天工作总结 冲刺第五天,学习了官方文档说明,学习并但是并未实现地图的放大与缩小 二.遇到的问题 对一些框架和API不太熟悉 未实现地图的放大与缩小 三.今日工作规划 深入学习有关视频与文档说明
- springboot学习入门之一---基本了解
1springboot基本了解 1.1概述 Spring Boot不是一门新技术,本质上就是spring. 特性: 1) 零配置(或很少配置) 2) 四个核心:(ASCA) 3.1)自动配置:spri ...
- IIS 中托管基于TCP绑定的WCF服务
IIS 中托管基于TCP绑定的WCF服务 一.创建一个基于TCP绑定的WCF服务 1.创建一个的简单的服务具体代码如下 服务契约定义 namespace SimpleService { // 注意: ...
- 将CAGradientLayer用作maskView的遮罩图层
将CAGradientLayer用作maskView的遮罩图层 说明 CAGradientLayer可以用作alpha遮罩,供maskView使用,这两种东西都是非常偏门的东西,但是,偏门不代表功能不 ...
- UITableView中cell点击的绚丽动画效果
UITableView中cell点击的绚丽动画效果 本人视频教程系类 iOS中CALayer的使用 效果图: 源码: YouXianMingCell.h 与 YouXianMingCell.m / ...
- UIView的无损截图
UIView的无损截图 说明 1. 烂大街的代码 2. 写成category后,方便直接从drawRect中获取绘制出来的图片 3. 可以直接绘制图片供按钮设置背景图片用 4. 无损截图(包括alph ...
- android-eclips中logcat不显示信息的问题解决
time:2015/11/20 1. logcat窗口不显示问题 解决: 参考[1] 2. logcat中不显示信息 (1)红米手机 (2)解决问题 * 有些文章提到重启eclipse,或者重启手机. ...
- Linux ulimit命令详解
ulimit 是一个计算机命令,用于shell启动进程所占用的资源,可用于修改系统资源限制 命令常用参数 -H 设置硬资源限制. -S 设置软资源限制. -a 显示当前所有的资源限制. -c size ...