Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to collapse a view in Xamarin.iOS in runtime?

fodorbalintfodorbalint Member ✭✭

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.

Best Answer


  • fodorbalintfodorbalint Member ✭✭
    edited January 8

    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.

Sign In or Register to comment.