NSLayoutAnchor无法在UITableViewCell中使用UIScrollViews

时间:2015-09-23 12:55:23

标签: swift uitableview uiscrollview autolayout ios9

我尝试重构代码,该代码以编程方式在UIScrollView上创建自动布局约束,UIScrollView是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匹配superview的高度但不匹配宽度,尽管它有一个子视图约束到UITableViewCell contentView的宽度。原始版本可以正常运行。

使用新的符号在标准UIView中构建相同的结构,因此我对一个错误感到怀疑,但我会非常感激地收到任何建议。

1 个答案:

答案 0 :(得分:0)

这似乎是Xcode中的某个错误。我实际上创建了一个新项目并从头开始构建自动布局代码(使用UILayoutAnchor)并且它可以工作。我确保代码是最初使用的代码的逐字副本,因此只能假设它在某个地方的系统中使用了gremlins。奇怪但Xcode有时候会像这样!

相关问题