I've figuratively pulled my hair out on this one. I read from this article "http://gregshackles.com/fluentlayout-2-5/" that FluentLayout now supports constraint editing/removing but it doesn't seem to work on my end. My scenario is to toggle the visibility of a textfield within a UIView when a button is clicked.
I have tried the following.
A. Modifying the height constraint
var height = isVisible ? textfield.Height().EqualTo(0) : textfield.WithSameHeight(textfieldContainer).Multiplier(1 / 3);
textfieldContainer.Add(textfield);
textfieldContainer.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
textfieldContainer.AddConstraints(
textfield.WithSameLeft(textfieldContainer).Plus(12),
textfield.WithSameTop(textfieldContainer).Plus(24),
textfield.WithSameWidth(textfieldContainer),
height
);
B. Using SetActive(false) - Tried this out of desperation
textfieldContainer.Add(textfield);
textfieldContainer.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
textfieldContainer.AddConstraints(
textfield.WithSameLeft(textfieldContainer).Plus(12).SetActive(!isVisible),
textfield.WithSameTop(textfieldContainer).Plus(24).SetActive(!isVisible),
textfield.WithSameWidth(textfieldContainer).SetActive(!isVisible),
textfield.WithSameHeight(textfieldContainer).WithMultiplier(1 / 4).SetActive(!isVisible)
);
Expected outcome
The textfield should be visible depending on the visibility
Actual outcome
The textfield's height never changes, thus it is always visible
My guess is that your
height
variable is set once during the page lifecycle, and does not get change after that point. A way of achieving what you need is the following:Firstly, bind your button click to a command that changes the state of a
boolean
on yourViewModel
, so that theboolean
's value changes when the button is clicked:bindingSet.Bind(yourButton).To(vm => vm.YourCommand);
If necessary, set
YourBoolean
to true during the construction of yourViewModel
, depending on whether your want the field to be hidden during page load. Now that theViewModel
property holds an accurate true/false state for whether yourUITextField
should be hidden, bind theUITextField
itself forHidden
to the boolean (you may need to use a value converter to reverse the value - ifHidden
is true, the view is invisible):Next, create
FluentLayout
variables that relate to both situations (your view being visible and being hidden), and apply both of them:And finally, bind the constraints for
Active
to theboolean
on yourViewModel
- note that one will need to be reversed with a converter:ReverseBooleanValueConverter
would look something like this:When
YourBoolean
is true, yourUITextField
should be invisible, and should have a height of0f
. WhenYourBoolean
is false, it should be visible, and should have one third the height of it's container.