iOS UI 按钮不遵守 TopAnchor 和 HeightAnchor
iOS UI Button Doesn't Respect TopAnchor and HeightAnchor
我正在尝试创建一个 UIViewController,其中有两个垂直堆叠在 UIView 底部的按钮(就像 iPod 上的 iOS 动作 sheet 外观)。出于某种原因,我的顶部按钮的顶部锚点永远粘在 UIViewController 的顶部。
我有底部按钮可以移动到控制器底部的正确位置。我通过像这样设置第二个按钮的顶部锚点来实现这一点:
NSLayoutConstraint.ActivateConstraints(new[] { this.secondButton.TopAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.BottomAnchor, -(this.secondButton.Frame.Height + 30f)) });
this.secondButton.UpdateConstraints();
我尝试对第一个按钮做类似的事情,因为 well.I 经历过 2 次顶部按钮卡在顶部的情况。
使用与设置第二个按钮相同类型的逻辑设置顶部锚点
NSLayoutConstraint.ActivateConstraints(new[] {
this.firstButton.HeightAnchor.ConstraintEqualTo(this.firstButton.Frame.Height),
this.firstButton.TopAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.BottomAnchor, -(2 * this.secondButton.Frame.Height + 60f)) });
this.firstButton.UpdateConstraints();
结果:
Top Anchor Stuck 1
通过将上面的 TopAnchor 行交换为此设置基于按钮二的底部锚点
this.firstButton.BottomAnchor.ConstraintEqualTo(this.buttonTwo.TopAnchor, -30f)
结果:Top Anchor Stuck 2
无论我如何设置按钮一的高度锚点和顶部/底部锚点,它都不会停止锚定到 viewcontroller 的最顶部。我已确保两个按钮的 TranslatesAutoresizingMaskIntoConstraints 都设置为 false,并且我检查了这两个按钮之前没有约束。有什么见解吗?谢谢!
我们需要查看您项目中的所有约束以找出原因和解决方案。我猜你在添加新约束时没有删除旧约束。
我给你写了一个例子来修复底部的两个按钮:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIButton firstButton = new UIButton(UIButtonType.System);
firstButton.SetTitle("first", UIControlState.Normal);
firstButton.Layer.BorderColor = UIColor.Green.CGColor;
firstButton.Layer.BorderWidth = 1.5f;
View.Add(firstButton);
UIButton secondButton = new UIButton(UIButtonType.System);
secondButton.SetTitle("second", UIControlState.Normal);
secondButton.SetTitleColor(UIColor.Red,UIControlState.Normal);
secondButton.Layer.BorderColor = UIColor.Yellow.CGColor;
secondButton.Layer.BorderWidth = 1.50f;
View.Add(secondButton);
firstButton.TranslatesAutoresizingMaskIntoConstraints = false;
secondButton.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddConstraints(new[] {
secondButton.HeightAnchor.ConstraintEqualTo(60),
secondButton.BottomAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.BottomAnchor,0),
secondButton.LeftAnchor.ConstraintEqualTo(View.LeftAnchor,20),
secondButton.RightAnchor.ConstraintEqualTo(View.RightAnchor,-20),
});
View.AddConstraints(new[] {
firstButton.HeightAnchor.ConstraintEqualTo(60),
firstButton.BottomAnchor.ConstraintEqualTo(secondButton.TopAnchor,-30),
firstButton.LeftAnchor.ConstraintEqualTo(View.LeftAnchor,20),
firstButton.RightAnchor.ConstraintEqualTo(View.RightAnchor,-20),
});
}
结果如下:
我正在尝试创建一个 UIViewController,其中有两个垂直堆叠在 UIView 底部的按钮(就像 iPod 上的 iOS 动作 sheet 外观)。出于某种原因,我的顶部按钮的顶部锚点永远粘在 UIViewController 的顶部。
我有底部按钮可以移动到控制器底部的正确位置。我通过像这样设置第二个按钮的顶部锚点来实现这一点:
NSLayoutConstraint.ActivateConstraints(new[] { this.secondButton.TopAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.BottomAnchor, -(this.secondButton.Frame.Height + 30f)) });
this.secondButton.UpdateConstraints();
我尝试对第一个按钮做类似的事情,因为 well.I 经历过 2 次顶部按钮卡在顶部的情况。
使用与设置第二个按钮相同类型的逻辑设置顶部锚点
NSLayoutConstraint.ActivateConstraints(new[] { this.firstButton.HeightAnchor.ConstraintEqualTo(this.firstButton.Frame.Height), this.firstButton.TopAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.BottomAnchor, -(2 * this.secondButton.Frame.Height + 60f)) }); this.firstButton.UpdateConstraints();
结果:
Top Anchor Stuck 1
通过将上面的 TopAnchor 行交换为此设置基于按钮二的底部锚点
this.firstButton.BottomAnchor.ConstraintEqualTo(this.buttonTwo.TopAnchor, -30f)
结果:Top Anchor Stuck 2
无论我如何设置按钮一的高度锚点和顶部/底部锚点,它都不会停止锚定到 viewcontroller 的最顶部。我已确保两个按钮的 TranslatesAutoresizingMaskIntoConstraints 都设置为 false,并且我检查了这两个按钮之前没有约束。有什么见解吗?谢谢!
我们需要查看您项目中的所有约束以找出原因和解决方案。我猜你在添加新约束时没有删除旧约束。
我给你写了一个例子来修复底部的两个按钮:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UIButton firstButton = new UIButton(UIButtonType.System);
firstButton.SetTitle("first", UIControlState.Normal);
firstButton.Layer.BorderColor = UIColor.Green.CGColor;
firstButton.Layer.BorderWidth = 1.5f;
View.Add(firstButton);
UIButton secondButton = new UIButton(UIButtonType.System);
secondButton.SetTitle("second", UIControlState.Normal);
secondButton.SetTitleColor(UIColor.Red,UIControlState.Normal);
secondButton.Layer.BorderColor = UIColor.Yellow.CGColor;
secondButton.Layer.BorderWidth = 1.50f;
View.Add(secondButton);
firstButton.TranslatesAutoresizingMaskIntoConstraints = false;
secondButton.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddConstraints(new[] {
secondButton.HeightAnchor.ConstraintEqualTo(60),
secondButton.BottomAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.BottomAnchor,0),
secondButton.LeftAnchor.ConstraintEqualTo(View.LeftAnchor,20),
secondButton.RightAnchor.ConstraintEqualTo(View.RightAnchor,-20),
});
View.AddConstraints(new[] {
firstButton.HeightAnchor.ConstraintEqualTo(60),
firstButton.BottomAnchor.ConstraintEqualTo(secondButton.TopAnchor,-30),
firstButton.LeftAnchor.ConstraintEqualTo(View.LeftAnchor,20),
firstButton.RightAnchor.ConstraintEqualTo(View.RightAnchor,-20),
});
}
结果如下: