如何创建返回 NSLayoutConstraint 的函数

Posted

技术标签:

【中文标题】如何创建返回 NSLayoutConstraint 的函数【英文标题】:how to create function that return NSLayoutConstraint 【发布时间】:2020-04-12 05:43:34 【问题描述】:

嘿,开发人员,我想问一下,我有一个重复的创建约束,然后我尝试创建一个返回 NSLayoutConstraint 的函数。然后我很困惑我应该返回什么,因为我的函数中有 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
    

【问题讨论】:

为什么需要函数返回NSLayoutConstraint?你想用这个约束做什么?请详细说明。 我想在 NSLayoutConstraints.activate([]) @hardikparmar 中激活它,因为函数内部重复了很多次,所以我想避免重复激活约束 我认为你想为 3 UIview 添加相同的约束。不是吗? 确实是@DilanAnuruddha :) 不需要从你的函数中返回一些东西。它在 VOID 心情下工作。 【参考方案1】:

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

您将返回类型添加到您的函数。向某些组件添加约束是 Void 类型。它不会返回任何内容。(所以删除它) 您在 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
        

【讨论】:

谢谢!! @DilanAnuruddha 为什么我不知道,哈哈。再次谢谢你 @ferryawijayanto 乐于提供帮助

以上是关于如何创建返回 NSLayoutConstraint 的函数的主要内容,如果未能解决你的问题,请参考以下文章

如何检查 NSLayoutConstraint 是不是动画

如何在 swift 的扩展中声明 NSLayoutConstraint 类型的变量

带有 NSLayoutConstraint 的 UITableView 在运行时崩溃时返回 null

如何在代码中创建与 @"H:|-10-[view]" 完全相同的 NSLayoutConstraint 对象?

如何将 swift NSLayoutConstraint activateConstraints 转换为目标 c?

以编程方式创建的动画 NSLayoutConstraint