旋转屏幕时的约束 [英] Constraints when rotate screen

查看:26
本文介绍了旋转屏幕时的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有约束的配置,我可以看到字段,但是当我旋转屏幕时,一切都没有了,我什么也没看到

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?

推荐答案

问题是由:

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

一旦你设置了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.

这篇关于旋转屏幕时的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆