swift5.x SnpKit 遇到的问题

2019-11-29  本文已影响0人  Bruce_XHG

1、更新约束的时候出现了闪退 updateConstraints
原代码

self.titleLabel.snp.makeConstraints { (make) in
    make.leading.top.equalToSuperview().offset(15)
    make.width..equalToSuperview().multipliedBy(0.9)
}

更新代码

self.titleLabel.snp.updateConstraints { (make) in
    make.width..equalToSuperview().multipliedBy(0.7)
}

闪退的提示信息说是找不到约束,这让人有点摸不着头脑啊,到底是咋回事呢.那就一步一步的找snpkit的源码,发现了下面这一段代码

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
}

snpkit的作者也说到了SnapKit's update method is an optimised update, it only updates the .constant value of the NSLayoutConstraint(s) for performance reasons

2、对于cell上子视图约束出现的问题,找了很久不知道是什么原因,最后发现是因为cell上某个子视图里面重写了layoutSubviews导致了问题的出现。
下面就出现的问题及截图说明一下

问题图: image.png
正确的图: image.png

其中的问题有两点:1、蓝色字体的button的top图层在父视图的top外;2、label的自适应高度展示不对;导致承载这两个控件的父视图的自适应高度也不对

重写了layoutSubviews是为了拿到另外一个子视图里面控件的frame,然而没想到对cell上其他子视图造成了影响。

上一篇下一篇

猜你喜欢

热点阅读