In Android we can use ViewStates.Gone on a view's Visibility, but in iOS if you set the Hidden property to false, the view will still take up space. I am using Auto Layout, so I thought of changing the dimension constraints like:
FilterLayout.HeightAnchor.ConstraintEqualTo(0f).Active = true;
It works once. But when I try to expand the view again by calling
FilterLayout.HeightAnchor.ConstraintEqualTo(0f).Active = false;
or just setting a new value:
FilterLayout.HeightAnchor.ConstraintEqualTo(200f).Active = true;
or writing both lines, the size does not change.
(FilterLayout's size is originally defined by its subviews, where the first subview's top matches its topMargin, and the last subview's bottom matches its bottomMargin.)
I even tried changing the neighboring container's constraint, so it doesn't attach to FilterLayout, and the result is, FilterLayout gets compressed, and changing the constraints back does not do anything.
On the top of the page is SearchLayout, below FilterLayout, below UserSearchList.
Hiding FilterLayout works:
FilterLayout.BottomAnchor.ConstraintEqualTo(UserSearchList.TopAnchor).Active = false;
UserSearchList.TopAnchor.ConstraintEqualTo(FilterLayout.BottomAnchor).Active = false;
SearchLayout.BottomAnchor.ConstraintEqualTo(UserSearchList.TopAnchor).Active = true;
UserSearchList.TopAnchor.ConstraintEqualTo(SearchLayout.BottomAnchor).Active = true;
Tried showing it again:
SearchLayout.BottomAnchor.ConstraintEqualTo(UserSearchList.TopAnchor).Active = false;
UserSearchList.TopAnchor.ConstraintEqualTo(SearchLayout.BottomAnchor).Active = false;
FilterLayout.BottomAnchor.ConstraintEqualTo(UserSearchList.TopAnchor).Active = true;
UserSearchList.TopAnchor.ConstraintEqualTo(FilterLayout.BottomAnchor).Active = true;
It doesn't matter if you set only one view's contraint, or both, the result is the same.
We need to manipulate the constraints we added before instead of creating a new constraint. This will cause conflict.
FilterLayout.HeightAnchor.ConstraintEqualTo(0f).Active = true;
This code means you want to add a new constraint whose constant is 0 and it will affect the current view immediately. However,
FilterLayout.HeightAnchor.ConstraintEqualTo(0f).Active = false;
This adds another constraint with active false. It won't disable the constraint you added before so the view won't change its height due to the former constraint.
Here is my code about how to find out the existed constraints. Then we could manipulate them directly:
partial void BtnClick(UIKit.UIButton sender) { if (isExpanded) { NSLayoutConstraint heightConstraint = null; foreach (NSLayoutConstraint constraint in FilterLayout.Constraints) { if (constraint.FirstAttribute == NSLayoutAttribute.Height) { heightConstraint = constraint; break; } } if (heightConstraint != null) { heightConstraint.Constant = 0; } else { FilterLayout.HeightAnchor.ConstraintEqualTo(0).Active = true; } isExpanded = false; } else { foreach (NSLayoutConstraint constraint in FilterLayout.Constraints) { if (constraint.FirstAttribute == NSLayoutAttribute.Height) { FilterLayout.RemoveConstraint(constraint); } } isExpanded = true; } } // The original state of the view is expanded bool isExpanded = true;
Answers
We need to manipulate the constraints we added before instead of creating a new constraint. This will cause conflict.
This code means you want to add a new constraint whose constant is 0 and it will affect the current view immediately. However,
This adds another constraint with active false. It won't disable the constraint you added before so the view won't change its height due to the former constraint.
Here is my code about how to find out the existed constraints. Then we could manipulate them directly:
Thanks, that's just the information I needed. I thought about cycling through the constraints, but it seemed more convenient that you can modify one by setting the Anchor property. Unfortunately the code is not made that way.