【问题标题】:How alignement two elements with contraints?如何对齐两个元素与约束?
【发布时间】:2019-12-05 02:27:40
【问题描述】:

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

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> 
|                    |         |          |         |   |
|                    +---------+          +---------+   |
|                                                       |
|                                                       |
|                                                       |

【问题讨论】:

    标签: c# ios xamarin


    【解决方案1】:

    我想这就是你想要的:

    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;
    
    }
    

    【讨论】:

    【解决方案2】:

    这里有 3 个视图:容器视图、大视图、小视图。
    1. 设置更大视图的 centerX,Y 约束等于容器视图的 centerX,Y 约束。
    2. 设置较小视图的 centerY 约束等于容器视图的 centerY。
    3.设置较小视图的右锚等于容器视图的右锚,偏移量-8(或任何你想要的) 希望对您有所帮助。

    【讨论】:

    • 视图大小相同
    • 我不明白。
    • 试试这个: 1. 设置 viewC 的顶部锚点等于容器视图的顶部锚点,偏移量为 120 2. 设置 viewC 的右锚点等于容器视图的右锚点,偏移量 -10 3. 设置 viewA 的中心 Y 等于viewC 的中心 Y。 4.设置viewA的右锚点等于viewC的左锚点,偏移量为x(x是你想要的)
    • 大方块是屏幕
    • 不管是什么大广场。屏幕只是一个大视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2012-03-31
    • 1970-01-01
    • 2020-07-11
    • 2022-11-19
    • 2021-11-04
    相关资源
    最近更新 更多