2017-12-27 23 views
0

스택 뷰에 새 뷰를 추가 할 때 스택 뷰 높이 제약을 추가 된 뷰의 양만큼 늘림으로써 스택 뷰 내부의 뷰 비율을 유지하려고합니다 . 뷰 제약 조건을 업데이트하는 방법에 대해 읽었지만 작동하지 않는 것 같습니다.스택 뷰에 뷰 추가 및 비율 유지를위한 높이 제약 조건 변경

self.stackView?.insertArrangedSubview(self.userErrorLabel, at: 2) 
self.heightConstraint = self.stackView?.heightAnchor.constraint(equalToConstant: 140 + self.stackView?.bounds.height) 
self.stackView?.addConstraints([self.heightConstraint!]) 
self.stackView?.setNeedsLayout() 

모든 조언은 매우 유용합니다 !!

+0

스토리 보드로도 할 수 있습니다. – User511

+0

제약 조건을 추가 할 때마다 먼저 모든 이전 제약 조건을 무효화해야하며 새로운 활성 제약 조건을 적용하면 다른 충돌이 발생할 수 있습니다. take [ref] (https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html) –

+0

하위 뷰를 추가 할 때마다 stackView에 새로운 제약 조건을 추가하면 안됩니다. 스택 뷰의 높이를 업데이트하십시오. – ridvankucuk

답변

0

XIB 또는 스토리 보드를 사용하고 있습니까? View/VC에 대한 Outlet으로 높이 제한을 등록한 다음 그 높이를 편집/증가시킬 수 있습니다. 하자가

@IBOutlet weak var stackViewHeight: NSLayoutConstraint! 

당신이해야 할 모든 단지

let currentStackHeight = stackViewHeight.constant 
stackViewHeight.constant = currentStackHeight + self.addedView.bounds.height 

하고 그냥 그런, 추가 또는 제약 조건을 제거 할 필요가 없습니다 당신이 높이 제약 조건을 등록 할 말