C# 如何将两个元素与约束对齐?
我需要对齐两个元素,一个在中间,另一个在右边C# 如何将两个元素与约束对齐?,c#,ios,xamarin,C#,Ios,Xamarin,我需要对齐两个元素,一个在中间,另一个在右边 a.WidthAnchor.ConstraintEqualTo(25).Active = true; a.HeightAnchor.ConstraintEqualTo(element1.WidthAnchor).Active = true; b.WidthAnchor.ConstraintEqualTo(30).Active = true; b.HeightAnchor.Con
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>
| | | | | |
| +---------+ +---------+ |
| |
| |
| |
+-----------------------------------------------------------------+(屏幕)
| ^ |
| 120 |
|五|
| +---------+ +---------+ |
| | | | | |
|| A | | C |
| | | | | |
| +---------+ +---------+ |
| |
| |
| |
我们这里有3个视图:容器视图、大视图、小视图。1.将较大视图的centerX,Y约束设置为等于容器视图的centerX,Y约束。
2.将较小视图的中心约束设置为容器视图的中心约束。
3.将较小视图的右定位点设置为偏移量为-8的容器视图的右定位点(或任意设置)
希望有帮助。我们这里有3个视图:容器视图、大视图、小视图。
1.将较大视图的centerX,Y约束设置为等于容器视图的centerX,Y约束。
2.将较小视图的中心约束设置为容器视图的中心约束。
3.将较小视图的右定位点设置为偏移量为-8的容器视图的右定位点(或任意设置)
希望能有所帮助。我想这就是你想要的:
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;
}
我想这就是你想要的:
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;
}
这些观点是相同的,我不明白。将viewC的上固定点设置为偏移120 2的容器视图的上固定点。将viewC的右定位点设置为偏移量为-10 3的容器视图的右定位点。将viewA的中心Y设置为viewC的中心Y。4.设置viewA的右锚定点等于viewC的左锚定点,偏移量为x(x为您想要的任何值)。无论大正方形是什么,大正方形都是屏幕。屏幕只是一个大视图。视图大小相同。我不明白。试试这个:1。将viewC的上固定点设置为偏移120 2的容器视图的上固定点。将viewC的右定位点设置为偏移量为-10 3的容器视图的右定位点。将viewA的中心Y设置为viewC的中心Y。4.设置viewA的右锚定点等于viewC的左锚定点,偏移量为x(x为您想要的任何值)。无论大正方形是什么,大正方形都是屏幕。屏幕只是一个大视图。你的答案很好,但我需要这个轴。Active=true;还有,你能补充一下你的答案吗please@JackHua-你能帮我一下吗!!!你的答案是好的,但我需要这个轴。Active=true;还有,你能补充一下你的答案吗please@JackHua-你能帮我一下吗!!!