How alignement two elements with contraints?

后端 未结 2 572
甜味超标
甜味超标 2021-01-28 07:24

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

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


        
相关标签:
2条回答
  • 2021-01-28 07:43

    I think this is what you want:

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

    And here is the result:

    Update:

    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;
    
    }
    
    0 讨论(0)
  • 2021-01-28 07:51

    we have 3 views here: container view, bigger view, smaller view.
    1. set bigger view's centerX,Y constraints equal to container view's centerX,Y constraints.
    2. set smaller view's centerY constraint equal to container view's centerY.
    3. set smaller view's right anchor equal to container view's right anchor with offset -8 (or whatever you want) Hope it helps.

    0 讨论(0)
提交回复
热议问题