2017-05-19 27 views
0

私はボタンプレスで以下のコードを実行しますが、下側の制約は従わないようです。サブビュー(bandCardView)は、親ビュー(formVw)の一番下をオーバーフローします。どのようにこれらの制約に従うことができますか?プログラム上の制約に従わない

@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)) 
    } 
+0

(時には、idkが必要なとき、またはなぜ必要なのか、いくつかのcustomViewsに必要です)....それぞれの制約の最後に 'isActive = true'と書いてください。また、 'translatesAutoresizingMaskIntoConstraints'が' false'に設定されていることを確認してください。それが動作するかどうかを確認する – Honey

+0

@Honeyサブビューの自動サイズ変更マスクです。私はいつもその部分を忘れています。あなたが提出して答えたら、私は受け入れます。 – steventnorris

+0

また、iOS 8をサポートしていない場合は、このように詳細な制約を作成するのを避け、代わりに 'topAnchor'、' bottomAnchor'などを使用することができます。[here](https://useyourloaf.com/blog/pain) -free-constraints-with-layout-anchors /)、または私の質問を参照してください[https://stackoverflow.com/questions/44008908/label-showing-top-of-screen-instead-of-being-on -the-inputaccessoryview) – Honey

答えて

2

あなたはfalseからtranslatesAutoresizingMaskIntoConstraintsセットを持っていることを確認してください。

また、iOS 9以降では、制約を記述するeasier wayがあります。

は、任意の定数せずに、単一の制約を追加します。

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) 

その後、あなたが実際に "アクティブ/制約を追加" するのを忘れています。

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... 

を、または単に


がさらにbandAccountView & formVwとの関係は、インスタンスされた第1のスニペット好きです - インスタンスを:それを修正するには、しなければならないのいずれかあなたはそれをやっている方法は良くありません。独自のクラスで制約を行うか、定数を調整するカスタムinitを作成する方がはるかに優れています

関連する問題