Toland Hon
Toland Hon

Reputation: 4619

Can I specify a AutoLayout constraint on iOS for spacing to the nearest visible neighbor?

I was curious if it is possible where I can add a constraint to the nearest visible (unhidden) neighbor.

Imagine that I have 3 UIViews:

[view1]-[view2]-[view3]

Lets say there's currently 10px between each views.

What I want is for view1 to be 10px from the nearest visible neighbor. So when I hide view2, view3 will move closer so that there will only be 10px between view1 and view3.

Is this possible via AutoLayout constraints?

Upvotes: 4

Views: 1234

Answers (1)

Aleks N.
Aleks N.

Reputation: 6247

A way to achieve this is to create an IBOutlet in the controller and associate your second view’s width constraint with it. Whenever you need to hide view2, modify the constant property of the constraint instead and set it to 0. Whenever you need to show the view back, restore the value of the constraint. This way view3 will dynamically move around.

Upvotes: 4

Related Questions