0
votes

I have this configuration with constraints, i can see the fields but when i rotate the screen, all is miss, i do not see anything

public override void ViewDidLoad()
    {

        field1.Frame = new CoreGraphics.CGRect(10, 10, 100, 20);
        field2.Frame = new CoreGraphics.CGRect(10, 50, 100, 20);
        field1.TranslatesAutoresizingMaskIntoConstraints = false;  
        field2.TranslatesAutoresizingMaskIntoConstraints = false; 

    View.AddConstraints(new[] {
            NSLayoutConstraint.Create(field1, NSLayoutAttribute.Width, NSLayoutRelation.Equal, View, NSLayoutAttribute.Width, 1, -200),
            NSLayoutConstraint.Create(field1, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 40),
            NSLayoutConstraint.Create(field1, NSLayoutAttribute.Top , NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, View.Bounds.Size.Height/2),
            NSLayoutConstraint.Create(field1, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1, 0)
        });

        View.AddConstraints(new[] {
            NSLayoutConstraint.Create(field2, NSLayoutAttribute.Width, NSLayoutRelation.Equal, field1, NSLayoutAttribute.Width, 1, 0),
            NSLayoutConstraint.Create(field2, NSLayoutAttribute.Height, NSLayoutRelation.Equal, field1, NSLayoutAttribute.Height, 1, 0),
            NSLayoutConstraint.Create(field2, NSLayoutAttribute.Top, NSLayoutRelation.Equal, field1, NSLayoutAttribute.Bottom, 1, 10),
            NSLayoutConstraint.Create(field2, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1, 0)
        });
}

but when I rotate the screen of my cellphone this configuration does not work, the textfield I do not see, how can I fix this wrong?

enter image description here

enter image description here

1
Can you add a screenshot to show us what you want to achieve? Where is the textfield? - nevermore
@JackHua-MSFT i edited my question see please !!! - user12327546
Does your problem resolved? - nevermore
@JackHua-MSFT i need your help with this please stackoverflow.com/questions/59187327/… - user12327546
Ok, I will check it later. - nevermore

1 Answers

0
votes

The problem was caused by:

NSLayoutConstraint.Create(field1, NSLayoutAttribute.Top , NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, View.Bounds.Size.Height/2),

Once you set the View.Bounds.Size.Height/2, it's a constant and won't update after you rotate the screen.

So the easilest solution is change it to :

NSLayoutConstraint.Create(field1, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterY, 1, -20),

Or you can update the constraints after rotating.