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

标签 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()

正如您所看到的,它没有正确移动到新的约束,在 reg 按钮下应该是 30

Not moving as expected

谢谢

最佳答案

尝试使用 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

帮助您不必一次又一次更新约束

关于ios - 如何以编程方式将顶部约束更改为布局中的另一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919412/

相关文章:

javascript - 在移动版 Safari 中针对多页 PDF 强制打印对话框

ios - Facebook Graph Api v2.0 me/photos 对除创建该应用程序的我以外的所有用户返回空

html - 从字符串中剥离 HTML 标签

swift - Dismiss ViewController 不释放内存

generics - F# 静态成员类型约束

MySQL:如果不存在则添加约束

ios - 在 iOS 上与 youtube-ios-player-helper 第三方库集成时出错

ios - RxSwift 只触发一次

swift - swift 2 中单元测试的 stub 方法

ios - 按钮到底部的自动布局