代码之家  ›  专栏  ›  技术社区  ›  Damodar

如何更新swift中的锚约束

  •  0
  • Damodar  · 技术社区  · 5 年前

    我想在iOS中创建一个类似Android的菜单。我在用 布局约束 设置 约束条件 . 当我试图更新左边时,我面临着这个问题 约束 单击按钮时的图像(它应该动画到单击按钮的位置)。有人能帮我吗? 它应该支持横向和纵向。

    我不想使用第三方代码,也不想使用 NSLayoutconstraint .

    Landscape image Potrait Image

    这是我的密码。

    class MenuViewController: UIViewController {
                var buttons: [UIButton] = []
                var imageView : UIImageView!
    
                override func viewDidLoad() {
                    super.viewDidLoad()
                }
    
                override func viewDidAppear(_ animated: Bool) {
                    super.viewDidAppear(animated)
                    addMenuItems()
                }
    
                func addMenuItems() {
    
                    for _ in 0...4 {
                        let button : UIButton = UIButton()
                        self.view.addSubview(button)
                        button.backgroundColor = UIColor.darkGray
                        button.addTarget(self, action: #selector(didSelectedButton(_:)), for: .touchUpInside)
                        self.buttons.append(button)
    
                    }
    
                    for (index, button) in buttons.enumerated() {
    
                        button.setTitle(" \(index)", for: .normal)
                        if index == 0 {
                            button.translatesAutoresizingMaskIntoConstraints = false
                            button.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
                            button.widthAnchor.constraint(greaterThanOrEqualToConstant: 1).isActive = true
                            button.heightAnchor.constraint(equalToConstant: 50).isActive = true
                            button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true
    
                            self.imageView = UIImageView()
                            self.view.addSubview(self.imageView)
                            self.imageView.backgroundColor = UIColor.red
    
                            self.imageView.translatesAutoresizingMaskIntoConstraints = false
                            self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0).isActive = true
                            self.imageView.widthAnchor.constraint(equalTo: button.widthAnchor, multiplier: 1).isActive = true
                            self.imageView.heightAnchor.constraint(equalToConstant: 4).isActive = true
                            self.imageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant:250).isActive = true
    
                        }
                        else {
    
                            let preButton = buttons\[index - 1\]
                            button.translatesAutoresizingMaskIntoConstraints = false
                            button.leftAnchor.constraint(equalTo: preButton.rightAnchor, constant: 0).isActive = true
                            button.widthAnchor.constraint(equalTo: preButton.widthAnchor, multiplier: 1).isActive = true
                            button.heightAnchor.constraint(equalToConstant: 50).isActive = true
                            button.topAnchor.constraint(equalTo: self.view.topAnchor, constant:200).isActive = true
                            if index == self.buttons.count - 1 {
                                button.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true
                            }
                        }
    
                    }
    
                }
    
                @objc func didSelectedButton(_ button:UIButton) {
                    UIView.animate(withDuration: 1, animations: {
                        self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor).isActive = true
                        self.imageView.layoutIfNeeded()
                    }, completion: nil)
                }
    
            }
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   vacawama    5 年前

    使用定位创建 NSLayoutContraint s.创建新的时,需要停用以前的。

    将此属性添加到 MenuViewController :

    var imageLeftConstraint: NSLayoutConstraint?
    

    第一次创建约束时进行设置:

    imageLeftConstraint = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 0)
    imageLeftConstraint?.isActive = true
    

    然后,在添加新约束之前,使用它停用先前的约束:

    @objc func didSelectedButton(_ button:UIButton) {
        imageLeftConstraint?.isActive = false
        imageLeftConstraint = self.imageView.leftAnchor.constraint(equalTo: button.leftAnchor)
        imageLeftConstraint?.isActive = true
        UIView.animate(withDuration: 1, animations: {
            self.view.layoutIfNeeded()
        }, completion: nil)
    }
    

    注: 你需要打电话 layoutIfNeeded() imageView (阿卡 self.view )因为两者之间的约束 button 图像浏览 将添加到他们的共同祖先( 自视图 )

    推荐文章