如何创建 return NSLayoutConstraint 的函数

how to create function that return NSLayoutConstraint

嘿开发者,我想问一下,我有一个重复的创建约束,然后我尝试创建一个 return NSLayoutConstraint 的函数。然后我弄糊涂了我应该 return 什么,因为我的函数中有 3 个不同的 UIView。在这里,我向您展示我的代码。

NSLayoutConstraint.activate([
            subtitlebl.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0),
            subtitlebl.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 18),

            profileImageView.topAnchor.constraint(equalTo: subtitlebl.bottomAnchor, constant: 24),
            profileImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 18),
            profileImageView.widthAnchor.constraint(equalToConstant: 68),
            profileImageView.heightAnchor.constraint(equalToConstant: 68),

            editButton.topAnchor.constraint(equalTo: subtitlebl.bottomAnchor, constant: 49),
            editButton.leadingAnchor.constraint(equalTo: profileImageView.trailingAnchor, constant: 12),
            editButton.widthAnchor.constraint(equalToConstant: 104),
            editButton.heightAnchor.constraint(equalToConstant: 20),

            profileNameStackView.topAnchor.constraint(equalTo: profileImageView.bottomAnchor, constant: 30),
            profileNameStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 24),
            profileNameStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -24),
            profileNameStackView.heightAnchor.constraint(equalToConstant: 74),

            phoneStackView.topAnchor.constraint(equalTo: profileNameStackView.bottomAnchor, constant: 30),
            phoneStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 24),
            phoneStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -24),
            phoneStackView.heightAnchor.constraint(equalToConstant: 74),

            createStackViewConstraint(stackView: profileNameStackView, topStackView: profileImageView, label: profileNameLbl, textFIeld: profileNameTextField)
)]

   private func createStackViewConstraint(stackView: UIStackView, topStackView: UIView, label: UILabel, textFIeld: UITextField) -> NSLayoutConstraint {
       stackView.topAnchor.constraint(equalTo: topStackView.bottomAnchor, constant: 30).isActive = true
       stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 24).isActive = true
       stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -24).isActive = true
       stackView.heightAnchor.constraint(equalToConstant: 74).isActive = true

       label.heightAnchor.constraint(equalToConstant: 18).isActive = true
       textFIeld.heightAnchor.constraint(equalToConstant: 48).isActive = true
    }

干得好,但是你的代码中几乎没有错误。

  • 您将 return 类型添加到您的 function.Adding 约束中,某些组件是 Void type.It 将 return 什么都没有。(所以删除它)
  • 您在 NSLayoutConstraint.activate 中添加了 profileNameStackView 约束并调用您的函数再次添加。

试试这个,确保你的组件

translatesAutoresizingMaskIntoConstraints = false

 NSLayoutConstraint.activate([
                subtitlebl.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0),
                subtitlebl.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 18),

                profileImageView.topAnchor.constraint(equalTo: subtitlebl.bottomAnchor, constant: 24),
                profileImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 18),
                profileImageView.widthAnchor.constraint(equalToConstant: 68),
                profileImageView.heightAnchor.constraint(equalToConstant: 68),

                editButton.topAnchor.constraint(equalTo: subtitlebl.bottomAnchor, constant: 49),
                editButton.leadingAnchor.constraint(equalTo: profileImageView.trailingAnchor, constant: 12),
                editButton.widthAnchor.constraint(equalToConstant: 104),
                editButton.heightAnchor.constraint(equalToConstant: 20),

    )]
    //add constraints to profileNameStackView
    createStackViewConstraint(stackView: profileNameStackView, topStackView: profileImageView, label: profileNameLbl, textFIeld: profileNameTextField)


    //add constraints to phoneStackView
    createStackViewConstraint(stackView: phoneStackView, topStackView: profileNameStackView, label: phoneLbl, textFIeld: phoneTextField)


//your function
       private func createStackViewConstraint(stackView: UIStackView, topStackView: UIView, label: UILabel, textFIeld: UITextField) -> NSLayoutConstraint {
           stackView.topAnchor.constraint(equalTo: topStackView.bottomAnchor, constant: 30).isActive = true
           stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 24).isActive = true
           stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -24).isActive = true
           stackView.heightAnchor.constraint(equalToConstant: 74).isActive = true

           label.heightAnchor.constraint(equalToConstant: 18).isActive = true
           textFIeld.heightAnchor.constraint(equalToConstant: 48).isActive = true
        }