0

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. Cell

1
  • IO9 + Rock & roll with UIStackView Commented Sep 21, 2017 at 10:43

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Use UIStackView then it will automatically adjust the height.

Another way is not remove the Label2, but make the height become 0, wrap Label2 in an UIView that have height from top Label2 to Label3, then change the height of that UIView to 0, visually it will be Label2 got removed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.