如何以编程方式将顶部约束更改为布局中的另一个项目

时间:2018-02-22 03:56:25

标签: ios swift constraints nslayoutconstraint

所以我在iOS中设置了一个facebook按钮作为..

main_view.addSubview(FBlogBut)

            NSLayoutConstraint(item: FBlogBut, attribute: .width, relatedBy: .equal, toItem: main_view, attribute: .width, multiplier: 0.95, constant: 0 ).isActive = true

            NSLayoutConstraint(item: FBlogBut, attribute: .centerX, relatedBy: .equal, toItem: loginView, attribute: .centerX, multiplier: 1, constant: 0 ).isActive = true

            topFBGuide = NSLayoutConstraint(item: FBlogBut, attribute: .top, relatedBy: .equal, toItem: login_button, attribute: .bottom, multiplier: 1, constant: 10 )

            NSLayoutConstraint(item: FBlogBut, attribute: .bottom, relatedBy: .greaterThanOrEqual, toItem: loginView, attribute: .bottom, multiplier: 1, constant: 10 ).isActive = true

            NSLayoutConstraint.activate([topFBGuide!])

            main_view.layoutIfNeeded()

First Layout works

按钮点击布局后,我执行以下操作..

print("flip to register")
    FBSDKLoginManager().logOut()
    NSLayoutConstraint.deactivate([topFBGuide!])
    topFBGuide2 = NSLayoutConstraint(item: FBlogBut, attribute: .top, relatedBy: .equal, toItem: register_button, attribute: .bottom, multiplier: 1, constant: 30 )
    NSLayoutConstraint.activate([topFBGuide2!])
    let buttonText = NSAttributedString(string: "Register with Facebook")
    FBlogBut.setAttributedTitle(buttonText, for: .normal)
    main_view.layoutIfNeeded()

因为你可以看到它没有正确地移动到新约束条件下,该约束条件应该在注册按钮

下为30

Not moving as expected

由于 [R

1 个答案:

答案 0 :(得分:0)

尝试使用StackView 检查下面的代码文件

而不是一次又一次地更改约束只是使用StackView并将StackView设置为StackView,因为我已经习惯了

class ConstraintsViewController: UIViewController {

    var TF1 = UITextField()
    var TF2 = UITextField()
    var TF3 = UITextField()
    var TF4 = UITextField()

    var toggleButton = UIButton()
    var FBLoginBtn = UIButton()
    var stackView = UIStackView()

    var isChecked = true

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        self.setUpView()
    }

    func setUpView()
    {
        ///Top Button as Toggle        
        toggleButton.backgroundColor = UIColor.lightGray
        toggleButton.setTitle("Let's Toggle", for: .normal)
        toggleButton.setTitleColor(UIColor.black, for: .normal)
        toggleButton.addTarget(self, action: #selector(ConstraintsViewController.buttonAction(sender:)), for: .touchUpInside)

        self.view.addSubview(toggleButton)
        toggleButton.translatesAutoresizingMaskIntoConstraints = false
        toggleButton.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
        toggleButton.heightAnchor.constraint(equalToConstant: self.view.frame.size.height*0.06).isActive = true
        toggleButton.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        toggleButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true


        ///TF That you want to add
        TF1.placeholder = "Email"
        TF1.widthAnchor.constraint(equalToConstant: 100).isActive = true
        TF1.heightAnchor.constraint(equalToConstant: 30).isActive = true

        TF2.placeholder = "Passowrd"
        TF2.widthAnchor.constraint(equalToConstant: 100).isActive = true
        TF2.heightAnchor.constraint(equalToConstant: 30).isActive = true

        TF3.placeholder = "ZipCode"
        TF3.widthAnchor.constraint(equalToConstant: 100).isActive = true
        TF3.heightAnchor.constraint(equalToConstant: 30).isActive = true

        TF4.placeholder = "Mobile Number"
        TF4.widthAnchor.constraint(equalToConstant: 100).isActive = true
        TF4.heightAnchor.constraint(equalToConstant: 30).isActive = true

        ///Stack View that will store all the required TF
        stackView.axis = UILayoutConstraintAxis.vertical
        stackView.distribution = UIStackViewDistribution.fill
        stackView.alignment = UIStackViewAlignment.center
        stackView.spacing = 20

        ///Add 2 TF in StackView
        stackView.addArrangedSubview(TF1)
        TF1.translatesAutoresizingMaskIntoConstraints = false
        stackView.addArrangedSubview(TF2)
        TF2.translatesAutoresizingMaskIntoConstraints = false

        self.view.addSubview(stackView)
        ///Properties
        stackView.translatesAutoresizingMaskIntoConstraints = false;
        ///Frame
        stackView.topAnchor.constraint(equalTo: toggleButton.bottomAnchor).isActive = true
        stackView.leadingAnchor.constraint(equalTo: toggleButton.leadingAnchor).isActive = true
        stackView.trailingAnchor.constraint(equalTo: toggleButton.trailingAnchor).isActive = true

        ///UIButton
        FBLoginBtn.backgroundColor = UIColor.lightGray
        FBLoginBtn.setTitle("Login to Facebook", for: .normal)
        FBLoginBtn.setTitleColor(UIColor.black, for: .normal)

        self.view.addSubview(FBLoginBtn)
        FBLoginBtn.translatesAutoresizingMaskIntoConstraints = false
        FBLoginBtn.topAnchor.constraint(equalTo: stackView.bottomAnchor).isActive = true
        FBLoginBtn.heightAnchor.constraint(equalToConstant: 50).isActive = true
        FBLoginBtn.leadingAnchor.constraint(equalTo: stackView.leadingAnchor).isActive = true
        FBLoginBtn.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
    }

    @objc func buttonAction(sender:UIButton)
    {
        isChecked = !isChecked
        if isChecked {
            self.stackView.removeArrangedSubview(TF3)
            self.stackView.removeArrangedSubview(TF4)
            TF3.removeFromSuperview()
            TF4.removeFromSuperview()
        } else {
            self.stackView.addArrangedSubview(TF3)
            self.stackView.addArrangedSubview(TF4)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

截图

首次加载时

enter image description here

按钮切换时

enter image description here

帮助您不要一次又一次地更新约束