gpt4 book ai didi

swift - NSLayoutAnchor 无法在 UITableViewCell 中使用 UIScrollViews

转载 作者:行者123 更新时间:2023-11-28 08:57:23 25 4
gpt4 key购买 nike

我正在尝试重构以编程方式在作为 UITableViewCell 的 contentView 的 subview 的 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 匹配父 View 的高度但不匹配宽度,尽管它有一个 subview 被限制为 UITableViewCell contentView 的宽度。原始版本没有错误。

使用新符号在标准 UIView 中构建相同的结构是可行的,因此我怀疑存在错误,但我们将不胜感激地收到任何建议。

最佳答案

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

关于swift - NSLayoutAnchor 无法在 UITableViewCell 中使用 UIScrollViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com