制約

2017-11-22 12 views
0

私は新しいUIViewを作成するため、このコードを使用していdynamycallyこのコード新しいUIViewController制約

@IBAction func newVCBtnPressed(_ sender: Any) { 
      let controller = DynamicVC() 
      show(controller, sender: sender) 
    } 

を使用して新しいUIViewControllerを作成しています:

override func loadView() { 

     view = UIView() 
     view.backgroundColor = .lightGray 
} 

結果にはview.lightGray背景色があります。

View with lightGray BG

私はプログラム的にカスタムUIViewのとセットアップに制約を追加したい、との結果に私は次の制約とのUIViewをしたい:

トップ:0

下:(view.frame.heightを* 0.9)

リーディング:0

末尾:(view.frame.width * 0.15)

幅:(view.frame.width * 0.85)

高さ:(view.frame.height * 0.1)

例:

enter image description here

ここでは私のコードである。

topMenuView = UIView() 
     topMenuView.backgroundColor = .red 

     view.addSubview(topMenuView) 
     topMenuView.translatesAutoresizingMaskIntoConstraints = false 
     setupConstraints(item: topMenuView, topC: 0, topToItem: view, bottomC: (view.frame.height*0.9), bottomToItem: view, widthC: (view.frame.width*0.85), heightC: (view.frame.height*0.1), leadingCon: 0, trailingCon: (view.frame.width*0.15)) 

私は制約のためにこの構成された関数を使用しています:

func setupConstraints(item:UIView, topC:CGFloat, topToItem:UIView, bottomC:CGFloat, bottomToItem:UIView, widthC:CGFloat, heightC:CGFloat, leadingCon:CGFloat, trailingCon:CGFloat) { 

      let topConstraint = NSLayoutConstraint(item: item, attribute: .top, relatedBy: .equal, toItem: topToItem, attribute: .bottom, multiplier: 1, constant: topC) 
      let bottomConstraint = NSLayoutConstraint(item: item, attribute: .bottom, relatedBy: .equal, toItem: bottomToItem, attribute: .top, multiplier: 1, constant: bottomC) 
      let widthConstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: widthC) 
      let heightConstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: heightC) 

      let leading = NSLayoutConstraint(item: item,attribute: .leading,relatedBy: .equal, toItem: view, attribute: .leadingMargin, multiplier: 1.0, constant: leadingCon) 

      let trailing = NSLayoutConstraint(item: item,attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailingMargin,multiplier: 1.0,constant: trailingCon) 

      view?.addConstraints([topConstraint, bottomConstraint, widthConstraint, heightConstraint, leading, trailing]) 

      NSLayoutConstraint.activate([topConstraint, bottomConstraint, widthConstraint, heightConstraint, leading, trailing]) 
     } 

しかし、私は灰色の背景を持つUIViewのみを受信し、赤い背景を持つ新しいUIViewは表示されません。

私は間違っていますか?

+0

一つのこと:

は遊び場を見るだけ* *制約を有効にします。ビューに追加しないでください。 – vacawama

+0

あるビューの* .top *を別のビューの* .bottom *に関連付けることは奇妙です。なぜ* .top *から* .top *と* .bottom *から* .bottom *にならないのですか? – vacawama

+0

また、オーバーライドされたloadViewメソッドでsuper.loadView()を呼び出します。 –

答えて

1

下端または高さと幅または後尾のみを指定する必要があります。そうしないと、ここで競合が発生します。

import PlaygroundSupport 
import UIKit 

class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     let red = UIView() 
     red.backgroundColor = .red 
     view.addSubview(red) 
     red.translatesAutoresizingMaskIntoConstraints = false 
     red.topAnchor.constraint(equalTo: view.topAnchor).isActive = true 
     red.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true 
     red.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.85).isActive = true 
     red.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1).isActive = true 
    } 
} 

PlaygroundPage.current.liveView = ViewController() 
+0

ありがとう!今それは明らかです。 – Rurom