I am a Xamarin.iOS beginner. I don't have much iOS native experience too. I am trying to hide and show a UIView which contains a UILabel and a UITextField. Though the hidden property is set to true for the view, it still occupies space in the screen. How do I remove this space? If I try to set the width constraints to 0 to hide it and then later, set it back to its original value to show it, it doesn't work. It continues to remain at 0.
Another question - will the view retain its state and behaviour when toggling on and off its visibility?
Thanks in advance for all the help..
Without your code it's difficult to tell the solutions. But from your question I can see few solutions like:
You can try changing constraint in main thread and after changing constraint you need to call LayoutIfNeeded()
[Foundation.Export("layoutIfNeeded")]
public virtual Void LayoutIfNeeded ()
Other than that you can use horizontal UIStackView
which will make UIView
's width to 0 while hiding and back to original while showing.
Even though you set the control's Hidden
to true, it still exists there just can't be seen. So if your second control's constraints depend on the first one, it will not change its position.
set it back to its original value to show it, it doesn't work. It continues to remain at 0.
Which kind of constraints are you using?
If you use NSLayoutAnchor
, you should disable the previous constraint before you want to modify it. For example:
//Define a field
NSLayoutConstraint WidthConstraint = null;
//When you want to modify the width, try to disable the previous one
if (WidthConstraint != null)
{
WidthConstraint.Active = false;
}
WidthConstraint = Control.WidthAnchor.ConstraintEqualTo(0);
WidthConstraint.Active = true;
If you use NSLayoutConstraint
, you can change the constant directly:
foreach (NSLayoutConstraint constraints in Control.Constraints)
{
if (constraints.FirstItem.IsEqual(Control) && constraints.FirstAttribute.Equals(NSLayoutAttribute.Width))
{
constraints.Constant = 0;
}
}