如何将两个要素与约束对齐? [英] How alignement two elements with contraints?

查看:89
本文介绍了如何将两个要素与约束对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要对齐两个元素,一个在中心,另一个在右侧

i want alignement two elements, one in the center and the other in right side

a.WidthAnchor.ConstraintEqualTo(25).Active = true;
            a.HeightAnchor.ConstraintEqualTo(element1.WidthAnchor).Active = true;

            b.WidthAnchor.ConstraintEqualTo(30).Active = true;
            b.HeightAnchor.ConstraintEqualTo(30).Active = true;



            a.TrailingAnchor.ConstraintEqualTo(element2.LeadingAnchor).Active = true;

            a.TopAnchor.ConstraintEqualTo(View.TopAnchor, 15).Active = true;
            b.TopAnchor.ConstraintEqualTo(View.TopAnchor, 15).Active = true;

            a.LeftAnchor.ConstraintEqualTo( View.CenterXAnchor).Active = true;
            b.RightAnchor.ConstraintEqualTo(View.RightAnchor).Active = true;

我尝试过,但这不起作用

i tried but this is not work

我要和这张照片一样

+------------------------------------------------------+(screen)
|                       ^                               |
|                      120                              |
|                       v                               |
|                    +---------+          +---------+   |
|                    |         |          |         |   |
|                    |    A    |          |    C    |<10> 
|                    |         |          |         |   |
|                    +---------+          +---------+   |
|                                                       |
|                                                       |
|                                                       |

推荐答案

我认为这是您想要的:

public partial class ViewController : UIViewController
{
    public ViewController (IntPtr handle) : base (handle)
    {
    }

    public UILabel labelOne { get; private set; }
    public UILabel labelTwo { get; private set; }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        labelOne = new UILabel();
        labelTwo = new UILabel();

        labelOne.Text = "test1";
        labelTwo.Text = "test2";
        labelOne.TextAlignment = UITextAlignment.Center;
        labelTwo.TextAlignment = UITextAlignment.Center;
        labelOne.BackgroundColor = UIColor.Red;
        labelTwo.BackgroundColor = UIColor.Blue;

        labelOne.TranslatesAutoresizingMaskIntoConstraints = false;
        labelTwo.TranslatesAutoresizingMaskIntoConstraints = false;

        View.Add(labelOne);
        View.Add(labelTwo);


        updateC();
    }

    public void updateC() {

        View.AddConstraints(new[] {
            NSLayoutConstraint.Create(labelOne, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 80),
            NSLayoutConstraint.Create(labelOne, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 80),
            NSLayoutConstraint.Create(labelOne, NSLayoutAttribute.Top , NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, 120),
            NSLayoutConstraint.Create(labelOne, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, View, NSLayoutAttribute.CenterX, 1, 0)
        });

        View.AddConstraints(new[] {
            NSLayoutConstraint.Create(labelTwo, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 80),
            NSLayoutConstraint.Create(labelTwo, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 80),
            NSLayoutConstraint.Create(labelTwo, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, labelOne, NSLayoutAttribute.CenterY, 1, 0),
            NSLayoutConstraint.Create(labelTwo, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, -10)
        });
    }
}

结果如下:

更新:

public void updateC() {

    labelOne.WidthAnchor.ConstraintEqualTo(30).Active = true;
    labelOne.HeightAnchor.ConstraintEqualTo(30).Active = true;

    labelTwo.WidthAnchor.ConstraintEqualTo(30).Active = true;
    labelTwo.HeightAnchor.ConstraintEqualTo(30).Active = true;

    labelOne.TopAnchor.ConstraintEqualTo(View.TopAnchor, 120).Active = true;
    labelTwo.CenterYAnchor.ConstraintEqualTo(labelOne.CenterYAnchor).Active = true;

    labelOne.CenterXAnchor.ConstraintEqualTo(View.CenterXAnchor).Active = true;
    labelTwo.RightAnchor.ConstraintEqualTo(View.RightAnchor, -10).Active = true;

}

这篇关于如何将两个要素与约束对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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