C# 编码约束未应用于子视图

C# 编码约束未应用于子视图,c#,xamarin,xamarin.ios,C#,Xamarin,Xamarin.ios,我在Xamarin.iOS中与约束作斗争 我创建了一个小测试应用程序,它应该只在我的主视图中添加一个绿色的UIView,并通过约束来定位它。我可以添加绿色视图,它将显示在右上角。好的,这很明显,因为我为视图设置了Frame,比如this.greenView.Frame=new CGRect(0,0,60,60)。我希望应用约束,使绿色框距离顶部、左侧和右侧大约15点(是的,我希望框大于初始值60) 限制: this.View.AddConstraints(新[]{ NSLayoutConstra

我在Xamarin.iOS中与约束作斗争

我创建了一个小测试应用程序,它应该只在我的主视图中添加一个绿色的
UIView
,并通过约束来定位它。我可以添加绿色视图,它将显示在右上角。好的,这很明显,因为我为视图设置了
Frame
,比如
this.greenView.Frame=new CGRect(0,0,60,60)。我希望应用约束,使绿色框距离顶部、左侧和右侧大约15点(是的,我希望框大于初始值60)

限制:

this.View.AddConstraints(新[]{
NSLayoutConstraint.Create(this.greenView,NSLayoutAttribute.Leading,NSLayoutRelation.Equal,this.View,NSLayoutAttribute.Leading,1,15),
NSLayoutConstraint.Create(this.greenView,NSLayoutAttribute.Trailing,NSLayoutRelation.Equal,this.View,NSLayoutAttribute.Trailing,1,15),
NSLayoutConstraint.Create(this.greenView,NSLayoutAttribute.Top,NSLayoutRelation.Equal,this.View,NSLayoutAttribute.Top,1,15)
});

当我运行该应用程序时,绿色框仍然位于右上角,大小为60x60

你们知道我做错了什么吗

谢谢你的帮助

完整代码:

    using CoreGraphics;
    using System;

    using UIKit;

    namespace iosStackViewCoding
    {
        public partial class ViewController : UIViewController
        {
            UIView greenView = null;

            public ViewController(IntPtr handle) : base(handle)
            {
            }

            public override void ViewDidLoad()
            {
                // add sub views
                this.AddGreenBox();

                base.ViewDidLoad();
                // Perform any additional setup after loading the view, typically from a nib.
            }

            public override void DidReceiveMemoryWarning()
            {
                base.DidReceiveMemoryWarning();
                // Release any cached data, images, etc that aren't in use.
            }

            public override void ViewDidLayoutSubviews()
            {
                // Layout the view if needed.
                this.View.LayoutIfNeeded();

                // apply initial size and position
                this.greenView.Frame = new CGRect(0, 0, 60, 60);

                // add constrains.
                this.View.AddConstraints(new[] {
                    NSLayoutConstraint.Create(this.greenView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Leading, 1, 15),
                    NSLayoutConstraint.Create(this.greenView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Trailing, 1, 15),
                    NSLayoutConstraint.Create(this.greenView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Top, 1, 15)
                });
            }

            private void AddGreenBox()
            {
                // create view and set background
                greenView = new UIView();
                greenView.BackgroundColor = UIColor.Green;

                // add to main view
                this.View.AddSubview(greenView);
            }
        }
    }

你的代码中有很多错误

1.我们不应该在
viewdilayoutsubviews
中设置约束! 此方法在UIViewController之前调用。视图在代码
视图中布局其子视图(修改UIKit.UIView.Bounds属性时触发)。每次调用AddConstraints
,表示它始终创建重复的约束

2.在设置约束之前,我们应该将
子视图.TranslatesAutoresizingMaskIntoConstraints
设置为false。

3.约束条件必须足以确定位置和尺寸。 然而,仅仅设置前导、尾随、顶部是不够的,还应该设置高度或底部

4我们最好不要将帧设置和自动布局混用。 使用autolayout放置视图就足够了,它的性能比框架设置好

最终代码:
谢谢!!!!!!这解决了我的问题。碰巧您没有任何关于如何正确调整视图大小的提示?因为stackView.SizeToFit()不会更改任何内容。。。
private void AddGreenBox()
{
    // create view and set background
    greenView = new UIView();
    greenView.BackgroundColor = UIColor.Green;
    this.View.AddSubview(greenView);

    greenView.TranslatesAutoresizingMaskIntoConstraints = false;

    View.AddConstraints(new[] {
        NSLayoutConstraint.Create(this.greenView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Leading, 1, 15),
        NSLayoutConstraint.Create(this.greenView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Trailing, 1, -15) ,
        NSLayoutConstraint.Create(this.greenView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Top, 1, 15),
        NSLayoutConstraint.Create(this.greenView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.View, NSLayoutAttribute.Bottom, 1, -15)
    });
}