我想要对齐两个元素,一个在中间,另一个在右边
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)
});
}
}
结果如下:
更新:
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/