I have cell with 3 labels. Labels have constraints to cell and to other labels. If I remove Label2 from superview then I will lost constraints and my cell will be changed. How to add constraints between Label1 and Label3 programmatically when I remove Label2.

-
IO9 + Rock & roll with UIStackViewPrashant Tukadiya– Prashant Tukadiya2017-09-21 10:43:50 +00:00Commented Sep 21, 2017 at 10:43
2 Answers
Here you have two Options
1) Use UIStackView (MUST BE IOS9 +)
2) Constraints
as UIStackView answer is already given so I am suggesting you a 2nd way to do it
Similar I have implemented for horizontal aligned label
Follow the steps
1) Let's Say your label is L1, L2, L3 , Give Equal Height to each Label (Please drag L2, And L3 to L1 and give equal Height)
2) Set Equal Height constraint priority to 999
3) As in your case L2 is optional (May be hidden or removed as per your scenario) so give HEIGHT Constraint to L2 with constant 0 with priority 750 (YOUR CONSTRAINTS NEVER BREAK )
4) Take IBOutlet of that constraint
5) And give leading trailing top bottom whatever you need in my case it is 0
6) Now You just need to change priority whenever you need to show hide that label
func setupViewForMyMemoriams (){
self.priority_Width_L2.priority = UILayoutPriorityDefaultLow
// OR
self.priority_Width_L2.priority = UILayoutPriorityRequired
}
Hope it is helpful to you