【问题标题】:Swift set constraints from code - View under viewSwift 从代码中设置约束 - 查看下视图
【发布时间】:2020-08-02 17:51:13
【问题描述】:

我正在使用这个扩展方法来设置我的视图锚:

@discardableResult
open func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
    
    translatesAutoresizingMaskIntoConstraints = false
    var anchoredConstraints = AnchoredConstraints()
    
    if let top = top {
        anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top)
    }
    
    if let leading = leading {
        anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
    }
    
    if let bottom = bottom {
        anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
    }
    
    if let trailing = trailing {
        anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
    }
    
    if size.width != 0 {
        anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width)
    }
    
    if size.height != 0 {
        anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height)
    }
    
    [anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach{ $0?.isActive = true }
    
    return anchoredConstraints
}

在我的应用程序中,我有两个标签,我想在这两个标签下设置一个 UITextField,其中包含以下代码:

widTextLabel.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0))
widTextLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -70).isActive = true

heiTextLabel.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: nil, bottom: nil, trailing: nil)
heiTextLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 70).isActive = true

widTextField.anchor(top: widTextLabel.bottomAnchor, leading: nil, bottom: nil, trailing: nil, size: .init(width: 50, height: 50))
widTextField.centerXAnchor.constraint(equalTo: widTextLabel.centerXAnchor).isActive = true

heiTextField.anchor(top: heiTextLabel.bottomAnchor, leading: nil, bottom: nil, trailing: nil, size: .init(width: 50, height: 50))
heiTextLabel.centerXAnchor.constraint(equalTo: heiTextLabel.centerXAnchor).isActive = true

我在控制台中收到此错误:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x6000015d9b40 "UITextField:0x7fdf8501a800.top"> and <NSLayoutYAxisAnchor:0x6000015d9b00 "UILabel:0x7fdf84e13cd0'test'.bottom"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'

【问题讨论】:

  • 例外是对的。 UILabel 测试放在哪里?建议再次进入 IB 并清除该标签的约束。
  • they have no common ancestor 这就是答案

标签: ios objective-c swift xcode constraints


【解决方案1】:

由于我的代表率低,无法发表评论。您是否已将所有这些视图作为子视图添加到您的主视图中?

我使用的是完全相同的扩展程序,昨天遇到了同样的问题。我通过浏览我的代码并在设置任何约束之前添加所有视图来解决它。明显的错误,但很容易在仓促中犯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多