내가 만드는거야 새로운 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가 있습니다.
나는 제약 프로그래밍 사용자 정의의 UIView 및 설정을 추가 할, 그리고 결과에 나는 다음과 같은 제약에 UIView를 원하는 :
최고 : 0
바닥 : (view.frame.height를 * 0.9)
선도 : 0
후행 : (view.frame.width * 0.15)
폭 (view.frame.width * 0.85)
신장 (view.frame.height * 0.1)
내 코드이다
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는 나타나지 않습니다.
내가 뭘 잘못하고 있니 ???
한 가지 :
놀이터를 볼에만 * * 제약 조건을 활성화합니다. 보기에 추가하면 안됩니다. – vacawama
하나의 뷰의 * .top *을 다른 뷰의 * .bottom *에 연관시키는 것은 이상한 일입니다. 왜 * .top * to * .top * 및 * .bottom * to * .bottom *일까요? – vacawama
또한 오버라이드 된 loadView 메소드에서 super.loadView()를 호출하십시오. –