Apply constrains programmatically

I struggle with constrains in Xamarin.iOS.

I created a little test application which should only add a green UIView to my main view and position it by constrains. I can add the green view and it will be displayed but only at the top right corner. Ok that's obvious since I set the Frame for the view like this.greenView.Frame = new CGRect(0, 0, 60, 60);. I than want to apply the constrains so that the green box is like 15point away from the top, left and right (yes, i want the box the be larger than the initial 60).

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

When I run the application the green box is still at the top right corner and is 60x60 large.

Do any of you have any idea what I am doing wrong?

Thx in advance for your help.

Full Code:

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

Best Answer

  • Accepted Answer

    I also posted the question on stackoverflow but the system won't let me post it here... Maybe because of the included link... I will post the answer as soon as I am allowed to. In the mean time, search for "Coded constrains not applied on sub view" on stackoverflow there you will find the answer.

Answers

Sign In or Register to comment.