Better AddConstraints method
See original GitHub issueCould 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<FluentLayout></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:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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! 😄
I don’t have a custom fork