question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better AddConstraints method

See original GitHub issue

Could you add this method ?

It extends the existing method to accept both FuentLayout and IEnumerable<FluentLayout>

Usage:

            View.AddConstraints(
                back.WithSameRectOf(View),

                topBack.WithSameTop(okButton).Minus(5),
                topBack.AtLeftRightOf(View),
                topBack.Bottom().EqualTo().TopOf(picker),

                actionButton.Height().EqualTo(70),
                actionButton.AtLeftRightOf(View),
                actionButton.AtBottomOf(View)
                );

Example:

        public static IEnumerable<FluentLayout> WidthEqualHeight(this UIView view, UIView previous=null)
        {
            yield return view.Width().EqualTo().HeightOf(previous ?? view);
        }

Source

        /// <summary>
        /// </summary>
        /// <param name="view"></param>
        /// <param name="fluentLayouts">Either FluentLayout or IEnumerable&lt;FluentLayout&gt;</param>
        public static void AddConstraints(this UIView view, params object[] fluentLayouts)
        {
            view.AddConstraints((fluentLayouts
                .Where(fluent => fluent != null)
                .OfType<FluentLayout>()
                .SelectMany(fluent => fluent.ToLayoutConstraints())
                ).Concat(fluentLayouts
                    .OfType<IEnumerable<FluentLayout>>()
                    .SelectMany(fluent => fluent.SelectMany(fluent2 => fluent2.ToLayoutConstraints()))
                ).ToArray());
        }

Other examples:

        public static IEnumerable<FluentLayout> WithSameRectOf(this UIView view, UIView parent, nfloat? margin = null)
        {
            return
                view.FullWidthOf(parent, margin)
                    .Concat(view.FullHeightOf(parent, margin))
                    .Concat(view.AtTopLeftOf(parent, margin));
        }

        public static IEnumerable<FluentLayout> AtTopLeftOf(this UIView view, UIView parentView, nfloat? margin = null)
        {
            var marg = margin.GetValueOrDefault(DefaultMargin);
            yield return view.Top().EqualTo().TopOf(parentView).Plus(marg);
            yield return view.Left().EqualTo().LeftOf(parentView).Plus(marg);
        }

        public static IEnumerable<FluentLayout> AtTopRightOf(this UIView view, UIView parentView, nfloat? margin = null)
        {
            var marg = margin.GetValueOrDefault(DefaultMargin);
            yield return view.Top().EqualTo().TopOf(parentView).Plus(marg);
            yield return view.Right().EqualTo().RightOf(parentView).Minus(marg);
        }

        public static IEnumerable<FluentLayout> AtLeftRightOf(this UIView view, UIView parentView, nfloat? margin = null)
        {
            var marg = margin.GetValueOrDefault(DefaultMargin);
            yield return view.Left().EqualTo().LeftOf(parentView).Plus(marg);
            yield return view.Right().EqualTo().RightOf(parentView).Minus(marg);
        }

        public static IEnumerable<FluentLayout> AtTopLeftRightOf(this UIView view, UIView parentView, nfloat? margin = null)
        {
            var marg = margin.GetValueOrDefault(DefaultMargin);
            yield return view.Top().EqualTo().TopOf(parentView).Plus(marg);
            yield return view.Left().EqualTo().LeftOf(parentView).Plus(marg);
            yield return view.Right().EqualTo().RightOf(parentView).Minus(marg);
        }

        public static IEnumerable<FluentLayout> BelowAtLeftRightOf(this UIView view, UIView belowView, UIView parentView, nfloat? belowMargin = null, nfloat? margin = null)
        {
            var marg = margin.GetValueOrDefault(DefaultMargin);
            yield return view.Top().EqualTo().BottomOf(belowView).Plus(belowMargin.GetValueOrDefault(DefaultMargin));
            yield return view.Left().EqualTo().LeftOf(parentView).Plus(marg);
            yield return view.Right().EqualTo().RightOf(parentView).Minus(marg);
        }

        public static IEnumerable<FluentLayout> EqualRectOf(this UIView view, UIView parentView, nfloat? margin = null)
        {
            var marg = margin.GetValueOrDefault(DefaultMargin);
            yield return view.Top().EqualTo().TopOf(parentView).Plus(marg);
            yield return view.Left().EqualTo().LeftOf(parentView).Plus(marg);
            yield return view.Right().EqualTo().RightOf(parentView).Minus(marg);
            yield return view.Bottom().EqualTo().BottomOf(parentView).Minus(marg);
        }

        public static IEnumerable<FluentLayout> WithSameCenter(this UIView view, UIView previous)
        {
            yield return view.WithSameCenterX(previous);
            yield return view.WithSameCenterY(previous);
        }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gshacklescommented, Feb 5, 2019

Closing this out for now since we wouldn’t be able to move ahead with this as-is and it sounds like you’ve got a custom fork anyway, but more than happy to continue to explore this if it still seems like you’d want to use/contribute - thanks! 😄

0reactions
softlioncommented, Mar 11, 2019

I don’t have a custom fork

Read more comments on GitHub >

github_iconTop Results From Across the Web

addConstraints(_:) | Apple Developer Documentation
Adds multiple constraints on the layout of the receiving view or its subviews.
Read more >
How to add constraints programmatically using Swift
Anchor style is the preferred method over NSLayoutConstraint Style, ... In my opinion xcode playground is the best place for learning adding ...
Read more >
Auto Layout in code: addConstraints with Visual Format ...
What's more, we're going to do this using a technique called Auto Layout Visual Format Language (VFL), which is kind of like a...
Read more >
Fastest way to add constraints - Gurobi Support
We are currently using Python to use Gurobi. We first make all the constraints without using Gurobi. We store a list of variables...
Read more >
ConstraintSystem.AddConstraints Method
The following code demonstrates how to use the ConstraintSystem object to solve the Zebra logic puzzle. For more examples, see Samples for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found