我试图重构以编程方式在UIScrollView上创建autolayout约束的代码,该约束是UITableViewCell的contentView的一个子视图,但正在获得一些不寻常的效果。
正在替换的代码片段是:
let scrollViewHorizontalConstraints =
NSLayoutConstraint.constraintsWithVisualFormat("H:|[sv]|",options: [], metrics: nil, views: viewDictionary)
viewConstraints.appendContentsOf(scrollViewHorizontalConstraints) /// scrollviewConstraints is an array!
let scrollViewVerticalConstraints =
NSLayoutConstraint.constraintsWithVisualFormat("V:|[sv]|", options: [], metrics: nil, views: viewDictionary)我应该能够将其替换为新的iOS9 UILayoutAnchor API,如下所示:
scrollView.leftAnchor.constraintEqualToAnchor(contentView.leftAnchor).active = true
scrollView.rightAnchor.constraintEqualToAnchor(contentView.rightAnchor).active = true
scrollView.topAnchor.constraintEqualToAnchor(contentView.topAnchor).active = true
scrollView.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor).active = true但是,此代码失败(没有错误)。约束没有正确构建--生成的scrollView与超级视图的高度匹配,但与宽度不匹配,尽管它的子视图受限于UITableViewCell contentView的宽度。原始版本工作正常,没有错误。
使用新的表示法在标准UIView中构建相同的结构是可行的,因此我对一个bug表示怀疑,但是任何建议都会被感激地收到。
发布于 2015-09-24 20:40:07
这似乎是Xcode中某个地方的bug。实际上,我已经创建了一个新项目,并从零开始构建了自动收费代码(使用UILayoutAnchor),它可以工作。我已经确保了代码是最初使用的代码的逐字副本,所以只能假设它在系统中的某个地方是小妖精。奇怪,但Xcode有时也会是这样的!
https://stackoverflow.com/questions/32740209
复制相似问题