c# - 如何用约束对齐两个元素?

标签 c# ios xamarin

我想要对齐两个元素,一个在中间,另一个在右边

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;

我试过了,但是不行

我想要和这张图一样的:

+------------------------------------------------------+(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)
        });
    }
}

结果如下:

enter image description here

更新:

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;

}

关于c# - 如何用约束对齐两个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59187327/

相关文章:

Xamarin 不尊重 JDK 位置 (VS2015)

c# - 如何在 Telegram Api 中订阅聊天更新?

ios - AWS iOS SDK AWSServiceManager 多种服务配置

xaml - 如何在 Xamarin Forms 中显示 ToolBarItem 图标的徽章计数

xaml - 动态资源未更新 .NET MAUI 中的 ImageButton 样式

ios - 如何让 Xcode 识别自定义模板

c# - 由于不兼容,无法安装 toast 通知

c# - 如何使用 UIElement 依赖属性实现 WPF 用户控件?

c# - 我可以使用代码契约来解决无法使用通用构造函数约束的问题吗?

ios - 使用 IBOutlets 加载 NIB/XIB 文件