NSLayoutAnchor 无法在 UITableViewCell 中使用 UIScrollViews
NSLayoutAnchor not working with UIScrollViews inside UITableViewCell
我正在尝试重构以编程方式在作为 UITableViewCell 的 contentView 的子视图的 UIScrollView 上创建自动布局约束的代码,但我得到了一些不寻常的效果。
被替换的代码片段是:
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 中构建相同的结构是可行的,因此我怀疑存在错误,但我们将不胜感激地收到任何建议。
这似乎是 Xcode 某处的错误。我实际上已经创建了一个新项目并从头开始构建了自动布局代码(使用 UILayoutAnchor)并且它有效。我已经确保该代码是最初使用的代码的逐字副本,因此只能假设它是系统某处的 gremlins。很奇怪,但 Xcode 有时会那样!
我正在尝试重构以编程方式在作为 UITableViewCell 的 contentView 的子视图的 UIScrollView 上创建自动布局约束的代码,但我得到了一些不寻常的效果。
被替换的代码片段是:
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 中构建相同的结构是可行的,因此我怀疑存在错误,但我们将不胜感激地收到任何建议。
这似乎是 Xcode 某处的错误。我实际上已经创建了一个新项目并从头开始构建了自动布局代码(使用 UILayoutAnchor)并且它有效。我已经确保该代码是最初使用的代码的逐字副本,因此只能假设它是系统某处的 gremlins。很奇怪,但 Xcode 有时会那样!