不能遵守程序性约束 [英] Programatic Constraints Not Obeyed

查看:91
本文介绍了不能遵守程序性约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在按下按钮时运行以下代码,但是似乎没有遵守最低要求。子视图(bandCardView)溢出了父视图(formVw)的底部。

I run the below code on a button press, however the bottom constraint does not seem to be obeyed. The subview (bandCardView) overflows the bottom of the parent view (formVw). How can I make these constraints obeyed?

 @objc private func cardBtnTouch(){
        self.bandAccountView?.bankBtn.setSelected(selected: false)
        self.bandAccountView?.cardBtn.setSelected(selected: true)

        self.bandAccountView?.selectLbl.isHidden = true
        for subview in self.bandAccountView!.formVw.subviews{
            subview.removeFromSuperview()
        }
        self.bandAccountView!.formVw.addSubview(bandCardView!)
        self.bandAccountView!.formVw.addConstraint(NSLayoutConstraint(item: self.bandAccountView!.formVw, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: bandCardView!, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0.0))
        self.bandAccountView!.formVw.addConstraint(NSLayoutConstraint(item: bandCardView!, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.bandAccountView!.formVw, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0.0))
        self.bandAccountView!.formVw.addConstraint(NSLayoutConstraint(item: bandCardView!, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.bandAccountView!.formVw, attribute: NSLayoutAttribute.leading, multiplier: 1.0, constant: 0.0))
        self.bandAccountView!.formVw.addConstraint(NSLayoutConstraint(item: self.bandAccountView!.formVw, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: bandCardView!, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: 0.0))
    }


推荐答案

确保已将 translatesAutoresizingMaskIntoConstraints 设置为 false

在iOS 9之后,还有一个轻松编写约束的方式

Also after iOS 9 there is an easier way of writing your constraints.

添加没有任何常数的单个约束:

self.bandAccountView!.formVw.addConstraint(formVw.topAnchor.constraint(equalTo:
 bandCardView.topAnchor)

添加带有常量的单个约束:

self.bandAccountView!.formVw.addConstraint(formVw.topAnchor.constraint(equalTo: 
bandCardView.topAnchor, constant: 10)

添加多个约束:

self.bandAccountView!.formVw.addConstraints([formVw.topAnchor.constraint(equalTo:
 bandCardView.topAnchor),formVw.bottomAnchor.constraint(equalTo:
 bandCardView.bottomAnchor),formVw.leadingAnchor.constraint(equalTo: 
 bandCardView.leadingAnchor),formVw.trailingAnchor.constraint(equalTo:
 bandCardView.trailingAnchor)]






注意:

如果您曾经写过:

self.bandAccountView!.formVw.leadingAnchor.constraint(equalTo:
formVw.leadingAnchor, constant: 0)

那么您实际上忘记了添加/激活约束。要修复它,您要么要做:

then you have actually forgot to "add/activate the constraint". To fix it you either have to do:

self.bandAccountView!.formVw.leadingAnchor.constraint(equalTo:
formVw.leadingAnchor, constant: 0).isActive = true

OR

let leadingConstraint = self.bandAccountView!.formVw.leadingAnchor.constraint(equalTo:
formVw.leadingAnchor, constant: 0)
leadingConstraint.isActive = true // do this whenever you need
leadingConstraint.isActive = false // if you don't need it...

或只是像第一个片段一样

or simply do like the first snippet

另外,<$ c之间的关系$ c> bandAccountView & formVw 是实例-实例变量,用它们的方式行事不好。最好在自己的类中进行约束,或者创建一个只为您调整常量的自定义init。

Additionally the relation between bandAccountView & formVw are instance--instance variable and they way you're doing it isn't good. It's much better to do the constraints in it's own class or create a custom init that would only adjust the constants for you.

这篇关于不能遵守程序性约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆