约束无法以编程方式工作 swift

constraints not working programmatically swift

func textFields() {
    let nameField = MDCFilledTextField()
    view.addSubview(nameField)
    nameField.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        nameField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        nameField.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        nameField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        nameField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
    ])
    nameField.placeholder = "Name"
}

我正在使用 google material 设计并创建一个 textfield.For 这个目的,我已经使用了这段代码,我想从左右两侧 space 但是它只能在左侧或右侧工作,我想从两侧 space。

在激活约束和删除使用框架之前将 nameField 添加为视图的子视图。

要添加填充,例如使用 centerXAnchor 和 widthAnchor + multiplier

func textFields() {
        
        let nameField = MDCFilledTextField()
        nameField.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(nameField)
                
        NSLayoutConstraint.activate([
            nameField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            nameField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            nameField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            nameField.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.9)
        ])
       
        nameField.placeholder = "Name"
        nameField.label.text = "Name"
        nameField.setFloatingLabelColor(.lightGray, for: MDCTextControlState.editing)
}

或者给leftAnchor和rightAnchor加上常量

func textFields() {
        
        let nameField = MDCFilledTextField()
        nameField.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(nameField)
                
        NSLayoutConstraint.activate([
            nameField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            nameField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            nameField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10),
            nameField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10)
        ])
      
        nameField.placeholder = "Name"
        nameField.label.text = "Name"
        nameField.setFloatingLabelColor(.lightGray, for: MDCTextControlState.editing)
}

您试图以错误的方式设置 MDCFilledTextField 位置。以下行告诉您的视图使用静态预定义帧大小:

let estimatedFrame = CGRect(x: 10, y: 200, width: UIScreen.main.bounds.width-20, height: 50)
let nameField = MDCFilledTextField(frame: estimatedFrame)

但在接下来的几行中,您告诉您的视图使用自动布局:

nameField.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    nameField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
    nameField.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
    nameField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
    nameField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])

所以现在您必须决定要使用哪一种,静态框架还是自动版式。如果您决定使用自动布局,则必须改进您的代码以消除您遇到的错误。首先,您需要将 translatesAutoresizingMaskIntoConstraints 设置为 true 而不是 false。

nameField.translatesAutoresizingMaskIntoConstraints = false

这一行将告诉您要为 nameField 使用自动布局而不是静态框架。此外,您需要先将视图添加到超级视图,否则您无法定义约束(因此出现错误)。所以你的代码变成:

func textFields() {
    let nameField = MDCFilledTextField()
    view.addSubview(nameField)
    nameField.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        nameField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10),
        nameField.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10),
        nameField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        nameField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
    ])
    nameField.placeholder = "Name"
}