如何减少这个Swift NSLayoutConstraint代码?

时间:2019-05-24 19:18:45

标签: swift nslayoutconstraint

在创建标签,按钮等之后。我使用该功能进行布局。在viewdidload中调用该函数,并且该函数非常有效。但是我对NSLayoutConstraint代码不满意。如何减少此代码?可以使用扩展程序吗?

iphne 5s and Xs

override func viewDidLoad() {
        super.viewDidLoad()
        buildUI()
    }
    private func buildUI() {
        view.backgroundColor = .lightGray

        view.addSubview(contentView)
        view.addSubview(usernameTextField)
        view.addSubview(passwordTextField)
        view.addSubview(forgotPasswordButton)
        view.addSubview(loginButton)
        view.addSubview(createAccountButton)

        NSLayoutConstraint.activate([
            contentView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 32),
            contentView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -32),
            contentView.heightAnchor.constraint(equalToConstant: 354),
            contentView.centerYAnchor.constraint(equalTo: view.centerYAnchor),

            usernameTextField.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 32),
            usernameTextField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -32),
            usernameTextField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 24),
            usernameTextField.heightAnchor.constraint(equalToConstant: 50),

            passwordTextField.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 32),
            passwordTextField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -32),
            passwordTextField.topAnchor.constraint(equalTo: usernameTextField.topAnchor, constant: 64),
            passwordTextField.heightAnchor.constraint(equalToConstant: 50),

            forgotPasswordButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            forgotPasswordButton.topAnchor.constraint(equalTo: passwordTextField.topAnchor, constant: 70),

            loginButton.bottomAnchor.constraint(equalTo: createAccountButton.bottomAnchor, constant: -46),
            loginButton.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 32),
            loginButton.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -32),
            loginButton.heightAnchor.constraint(equalToConstant: 50),

            createAccountButton.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
            createAccountButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -30),

            ])
    }

1 个答案:

答案 0 :(得分:0)

这是我使用的扩展名。随附文件。我并没有使用太多中心锚,但是如果您真的需要它们,可以轻松地将它们添加到扩展中。

extension UIView {
    /**
     Anchors a view using the input constraints.
     - Parameter top: The top constraint.
     - Parameter leading: The leading or left contraint.
     - Parameter bottom: The bottom constraint.
     - Parameter trailing: The trailing or right contraint.
     - Parameter padding: The padding to be applied to the constraints. Requires init with UIEdgeInsets.
     - Parameter size: The size to be added to the view. Requires init with CGSize. If all other constraints are set, sizes do not have any effect.
     */

    func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) {
        translatesAutoresizingMaskIntoConstraints = false
        if let top = top { topAnchor.constraint(equalTo: top, constant: padding.top).isActive = true }
        if let leading = leading { leadingAnchor.constraint(equalTo: leading, constant: padding.left).isActive = true }
        if let bottom = bottom { bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom).isActive = true }
        if let trailing = trailing { trailingAnchor.constraint(equalTo: trailing, constant: -padding.right).isActive = true }
        if size.width != 0 { widthAnchor.constraint(equalToConstant: size.width).isActive = true }
        if size.height != 0 { heightAnchor.constraint(equalToConstant: size.height).isActive = true }
    }
}

以下是如何使用它以及如何明显减少行数的示例:

usernameTextField.anchor(top: contentView.topAnchor, leading: contentView.leadingAnchor, bottom: nil, trailing: contentView.trailingAnchor, padding: .init(top: 24, left: 32, bottom: 0, right: 32), size: .init(width: 0, height: 50))