如何在自定义 UIView 中添加约束?

How to add constrains inside custom UIView?

我想给 UILabel 添加约束 我的自定义 UIView class:

class LabelView: UIView {

    public var label: UILabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = frame
        prepareUI()
    }  
    func prepareUI() {     
        self.label.text = "SOME TEXT"
        self.label.sizeToFit()
        self.label.adjustsFontSizeToFitWidth = true
        self.label.textAlignment = .center
        self.addSubview(self.label)
        
        self.translatesAutoresizingMaskIntoConstraints = false
        self.label.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.label.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true
        self.label.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.2).isActive = true
    }
}

在 运行 之后出现以下错误,UILabel 位于 UIView 的左上角:

2021-02-13 02:32:26.698694+1000 SwiftHello[96793:1625524] [LayoutConstraints] 无法同时满足约束条件。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档) ( “”, “”, “”, “” )

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。 2021-02-13 02:32:26.698920+1000 SwiftHello[96793:1625524] [LayoutConstraints] 无法同时满足约束。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档) ( “”, “”, “”, “” )

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。 2021-02-13 02:32:26.702639+1000 SwiftHello[96793:1625524] [LayoutConstraints] 无法同时满足约束。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档) ( “”, “SwiftHello.LabelView:0x1426167c0.width (活动)>”, “UIView:0x14260e930.width (active)>”, “” )

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。 2021-02-13 02:32:26.702816+1000 SwiftHello[96793:1625524] [LayoutConstraints] 无法同时满足约束。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档) ( “”, “SwiftHello.LabelView:0x1426167c0.height (active)>”, “UIView:0x14260e930.height (active)>”, “” )

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。

你漏掉了这一行:

self.label.translatesAutoresizingMaskIntoConstraints = false

您在 self 上设置了它,但没有在 label 上设置。