제약

2017-11-22 14 views
0

내가 만드는거야 새로운 UIViewController dynamycally 나는 새로운 UIView의 창조를 위해이 코드를 사용하고 새로운 UIViewController에서이 코드제약

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

를 사용하여 :

override func loadView() { 

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

결과로 view.lightGray backgroundcolor가 있습니다.

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 * to * .top * 및 * .bottom * to * .bottom *일까요? – vacawama

+0

또한 오버라이드 된 loadView 메소드에서 super.loadView()를 호출하십시오. –

답변

1

하단 OR 높이와 너비 또는 후행 만 지정해야합니다. 그렇지 않으면 여기서 충돌이 발생합니다.

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