Example #1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            Title = "Animation";

            var box1 = new UIView {
                BackgroundColor = UIColor.FromRGB(250, 250, 250)
            };
            var box2 = new UIView {
                BackgroundColor = UIColor.FromRGB(10, 10, 10)
            };
            var box3 = new UIView {
                BackgroundColor = UIColor.FromRGB(240, 240, 240)
            };
            var box4 = new UIView {
                BackgroundColor = UIColor.FromRGB(240, 240, 240)
            };

            // set constraints as usual
            ConstraintHelper

            .Attach(box1)
            .Top().Left().Right().Height(1f)
            .Attach(box2)
            .Width(10f).Height(10f).Center().Middle()
            .Attach(box3)
            .Top(20f).Left().Height(40f).Width(0f)
            .Attach(box4)
            .Bottom(20f).Right().Height(40f).Width(0f)

            // layout constraints (equal to LayoutIfNeeded)
            .UpdateConstraints();


            UIView.Animate(0.8, 0, UIViewAnimationOptions.CurveEaseOut,
                           () => {
                box1.BackgroundColor    = UIColor.FromRGB(10, 10, 10);
                box2.BackgroundColor    = UIColor.FromRGB(128, 128, 128);
                box2.Layer.CornerRadius = 75f;

                // change the constraints
                ConstraintHelper

                .WorkWith(box1)
                .RemoveHeightConstraint().HeightOfParent(0.5f)
                .WorkWith(box2)
                .RemoveWidthConstraint().Width(150f)
                .RemoveHeightConstraint().Height(150f)
                .WorkWith(box3)
                .RemoveWidthConstraint().WidthOfParent(0.8f)
                .WorkWith(box4)
                .RemoveWidthConstraint().WidthOfParent(0.8f)

                // layout constraints again (equal to LayoutIfNeeded)
                .UpdateConstraints();
            }, null
                           );
        }
Example #2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            Title = "Sizing";

            var box1 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 180, 100)
            };
            var box2 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 180, 150)
            };
            var box3 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 180, 200)
            };
            var box4 = new UIView {
                BackgroundColor = UIColor.FromRGB(140, 140, 140)
            };
            var box5 = new UIView {
                BackgroundColor = UIColor.FromRGB(160, 160, 160)
            };
            var box6 = new UIView {
                BackgroundColor = UIColor.FromRGB(180, 180, 180)
            };

            ConstraintHelper
            .Attach(box1)
            .Width(80f).Height(100f)                             // setting size by fixed values
            .Top().Left()

            .Attach(box2)
            .WidthOf(box1).HeightOf(box1)                             // setting size to the same of another element
            .Top().LeftOf(box1)

            .Attach(box3)
            .WidthOf(box2, 2f).HeightOf(box2)                             // setting size to the same of another element with modifier
            .Top().LeftOf(box2).StackTop()

            .Attach(box4)
            .WidthOfParent(0.25f)                          // setting sizes of parent containers with modifier
            .HeightFromWidth()                             // use views height as width
            .Top().Left()

            .Attach(box5)
            .WidthOfParent(0.5f)
            .HeightFromWidth()                             // use views width as height
            .Top().LeftOf(box4)

            .Attach(box6)
            .HeightOf(box4)
            .WidthFromHeight()
            .Top().Right();
        }
Example #3
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            Title = "Stacking";

            int amount = 15;

            int max        = amount * amount;
            int color      = 255;
            int colorSteps = (int)Math.Floor((double)(255 / max));


            // In this case we work with as scrollview
            _scrollview = new UIScrollView();
            ConstraintHelper.Attach(_scrollview).Top().Right().Bottom().Left();

            // We get us a new constraint helper to work with the scroll view as parent
            var scrollViewConstraintHelper = new ConstraintHelper(_scrollview);

            // Stacking makes it easier to position elements around each other without needing them to know
            // it works exactly the same way as AboveOf, BelowOf, LeftOf and RightOf

            for (int i = 1; i < max + 1; i++)
            {
                scrollViewConstraintHelper
                .Attach(new UIView {
                    BackgroundColor = UIColor.FromRGB(color, color, color)
                })
                .WidthOfParent((float)1 / amount)
                .HeightFromWidth()
                .Top()
                .Left()
                .StackLeft();                         // makes next call of Left behave like LeftOf currrent item

                if (i % amount == 0)
                {
                    scrollViewConstraintHelper
                    .StackTop()                             // makes next call of Top behave like LeftOf currrent item
                    .ResetLeftStack();                      // afterwards Left will position according to parents left
                }

                color -= colorSteps;
            }

            // Since we use a scroll view, it is important to set a bottom constraint
            // for the last element otherwise scrolling will not work
            scrollViewConstraintHelper.Bottom(); // we still work on the last attached element
        }
Example #4
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            Title = "Examples";

            _buttonSizing = new AutoRoundButton("Sizing")
            {
                BackgroundColor = UIColor.FromRGB(210, 210, 210)
            };
            _buttonPositioning = new AutoRoundButton("Positioning")
            {
                BackgroundColor = UIColor.FromRGB(180, 180, 180)
            };
            _buttonStacking = new AutoRoundButton("Stacking")
            {
                BackgroundColor = UIColor.FromRGB(150, 150, 150)
            };
            _buttonMargin = new AutoRoundButton("Margin")
            {
                BackgroundColor = UIColor.FromRGB(120, 120, 120)
            };
            _buttonAnimation = new AutoRoundButton("Animation")
            {
                BackgroundColor = UIColor.FromRGB(90, 90, 90)
            };

            ConstraintHelper

            .Attach(_buttonSizing)
            .Top(5f).Center(-50f).HeightFromWidth()

            .Attach(_buttonPositioning)
            .Top(5f).Center(50f).HeightFromWidth().StackTop()

            .Attach(_buttonStacking)
            .Top(-32).Center(-42).HeightFromWidth()

            .Attach(_buttonMargin)
            .Top(10f).Center(42f).HeightFromWidth().StackTop()

            .Attach(_buttonAnimation)
            .Top(-18).Center(-25f).HeightFromWidth().StackTop();
        }
Example #5
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            Title = "Positioning";

            var box1 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 100, 100)
            };
            var box2 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 100, 100)
            };
            var box3 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 100, 100)
            };
            var box4 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 100, 100)
            };

            var box5 = new UIView {
                BackgroundColor = UIColor.FromRGB(100, 100, 100)
            };

            var box6 = new UIView {
                BackgroundColor = UIColor.FromRGB(130, 190, 80)
            };
            var box7 = new UIView {
                BackgroundColor = UIColor.FromRGB(130, 190, 80)
            };
            var box8 = new UIView {
                BackgroundColor = UIColor.FromRGB(130, 190, 80)
            };
            var box9 = new UIView {
                BackgroundColor = UIColor.FromRGB(130, 190, 80)
            };

            var box10 = new UIView {
                BackgroundColor = UIColor.FromRGB(0, 100, 250)
            };
            var box11 = new UIView {
                BackgroundColor = UIColor.FromRGB(0, 100, 250)
            };
            var box12 = new UIView {
                BackgroundColor = UIColor.FromRGB(0, 100, 250)
            };
            var box13 = new UIView {
                BackgroundColor = UIColor.FromRGB(0, 100, 250)
            };


            ConstraintHelper

            .Attach(box1)
            .Top()                             // position to the top of the parent view
            .Left()                            // position to the left of the parent view
            .Width(50f).Height(50f)

            .Attach(box2)
            .Bottom()                           // position to the bottom of the parent view
            .Left()                             // position to the left of the parent view
            .Width(50f).Height(50f)

            .Attach(box3)
            .Top()                             // position to the top of the parent view
            .Right()                           // position to the right of the parent view
            .Width(50f).Height(50f)

            .Attach(box4)
            .Bottom()                            // position to the bottom of the parent view
            .Right()                             // position to the right of the parent view
            .Width(50f).Height(50f)


            .Attach(box5)
            .Center()                             // position to the vertical center of the parent view
            .Middle()                             // position to the horizontal center of the parent view
            .Width(50f).Height(50f)


            .Attach(box6)
            .AboveOf(box5)                             // position above another view
            .Center()
            .Width(50f).Height(50f)

            .Attach(box7)
            .BelowOf(box5)                             // position below another view
            .Center()
            .Width(50f).Height(50f)

            .Attach(box8)
            .RightOf(box5)                             // position on the right of another view
            .Middle()
            .Width(50f).Height(50f)

            .Attach(box9)
            .LeftOf(box5)                             // position on the left of another view
            .Middle()
            .Width(50f).Height(50f)


            .Attach(box10)
            .AboveOf(box6)
            .RightOf(box8)
            .Width(50f).Height(50f)

            .Attach(box11)
            .AboveOf(box6)
            .LeftOf(box9)
            .Width(50f).Height(50f)

            .Attach(box12)
            .BelowOf(box7)
            .RightOf(box8)
            .Width(50f).Height(50f)

            .Attach(box13)
            .BelowOf(box7)
            .LeftOf(box9)
            .Width(50f).Height(50f);
        }
Example #6
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            Title = "Margin";

            var box1 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(200, 200, 200)
            };
            var box2 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(210, 210, 210)
            };
            var box3 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(220, 220, 220)
            };

            var box4 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(230, 230, 230)
            };
            var box5 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(230, 230, 230)
            };

            var box6 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(240, 240, 240)
            };
            var box7 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(240, 240, 240)
            };
            var box8 = new UIView()
            {
                BackgroundColor = UIColor.FromRGB(240, 240, 240)
            };

            var helper = new UIView();

            // Margins are handeld similarly as they are in CSS. Methods for them also follow the same rules.
            // Margins will not collapse and will have affect when you position a an element around another
            ConstraintHelper

            .Attach(box1)
            .Left(10f)                            // add left margin
            .Right(10f)                           // add right margin
            .Top(10f)                             // add top margin
            .Height(40f)
            .StackTop()

            .Attach(box2)
            .SetMargin(10f)                     // setting all margin at once
            .Left()                             // the element still has to be positioned
            .Right()
            .Top()
            .Height(40f)
            .StackTop()

            .Attach(box3)
            // margin don't collaps, the bottom margin of the previous element takes effect, so no margin top
            .SetMargin(0, 10f)                  // there is multiple signatures (N, E, S, W), (N&S, E&W), (All the same)
            .Left()                             // the element still has to be positioned
            .Right()
            .Top()
            .Height(40f)
            .StackTop()

            // you can modify width and height you inherit of another view
            .Attach(box4)
            .SetMargin(10f, 5f, 0, 10f)
            .Top()
            .Left()
            .WidthOfParent(0.5f, -15f)                             // left and right margin as modifier
            .Height(45f)

            .Attach(box5)
            .SetMargin(10f, 10f, 0, 5f)
            .Top()
            .Right()
            .WidthOfParent(0.5f, -15f)                             // left and right margin as modifier
            .Height(45f)
            .StackTop()

            // don't be afraid to use helper views
            .Attach(helper)
            .WidthOfParent(1f, -10f)
            .Top(10f)
            .Left();

            (new ConstraintHelper(helper))
            .Attach(box6)
            .SetMargin(0, 0, 0, 10f)
            .Top()
            .Left().StackLeft()
            .WidthOfParent(0.3333f, -10f)
            .Height(45f)

            .Attach(box7)
            .SetMargin(0, 0, 0, 10f)
            .Top()
            .Left().StackLeft()
            .WidthOfParent(0.3333f, -10f)
            .Height(45f)

            .Attach(box8)
            .SetMargin(0, 0, 0, 10f)
            .Top()
            .Left().StackLeft()
            .WidthOfParent(0.3333f, -10f)
            .Height(45f)
            // make it a habit of pinning the last element to the bottom
            // if the parents height is not predefined (specialy if you use scroll views!)
            .Bottom();
        }