Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Can't get layout constraints to work in code

$
0
0

Hello, I'm having trouble getting constraints working. I have a view with a list of views in children. I want them to be laid out in a vertical stack. So, I'm using the method below, but I get a blank view when I call it. Trying to get my head around how this should work with this example.

What have I missed?

Cheers.

`public void BuildConstraints() {

        this.RemoveConstraints(this.Constraints);
        this.TranslatesAutoresizingMaskIntoConstraints = false;

        const int hpad = 4;
        UIView prev = null;
        foreach (var v in Children)
        {
            v.TranslatesAutoresizingMaskIntoConstraints = false;

            if (prev != null)
            {
                this.AddConstraint (NSLayoutConstraint.Create (
                    v, NSLayoutAttribute.Top, 
                    NSLayoutRelation.Equal, 
                    prev, NSLayoutAttribute.Bottom, 
                    1, hpad));
            } else
            {
                this.AddConstraint (NSLayoutConstraint.Create (
                    v, NSLayoutAttribute.Top, 
                    NSLayoutRelation.Equal, 
                    this, NSLayoutAttribute.Top, 
                    1, hpad));
            }
            prev = v;
        }

        var constraints = this.Constraints;
        foreach (var c in Constraints)
            Console.WriteLine (c);
    }`

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>