public static void AddConstraints(this UIView view, IEnumerable <FluentLayout> fluentLayouts) => view.AddConstraints(fluentLayouts .Where(fluent => fluent != null) .Select(fluent => fluent.Constraint.Value) .ToArray());
void SetStackConstraints() { if (nativeView == null) { return; } CreateNativeViewsForChildren(); var horizontal = Orientation == StackOrientation.Horizontal; var lefts = horizontal ? children.Where(x => x.Layout.HorizontalAlignment == HorizontalAlignment.Left).ToList() : children.Where(x => x.Layout.VerticalAlignment == VerticalAlignment.Top).ToList(); var centers = horizontal ? children.Where(x => x.Layout.HorizontalAlignment == HorizontalAlignment.Center).ToList() : children.Where(x => x.Layout.VerticalAlignment == VerticalAlignment.Center).ToList(); var rights = horizontal ? children.Where(x => x.Layout.HorizontalAlignment == HorizontalAlignment.Right).ToList() : children.Where(x => x.Layout.VerticalAlignment == VerticalAlignment.Bottom).ToList(); var newConstraints = new List <NSLayoutConstraint> (); // // Align the major axis // var leftAttr = horizontal ? NSLayoutAttribute.Left : NSLayoutAttribute.Top; var rightAttr = horizontal ? NSLayoutAttribute.Right : NSLayoutAttribute.Bottom; //var centerAttr = horizontal ? NSLayoutAttribute.CenterX : NSLayoutAttribute.CenterY; Action <NativeView, NSLayoutAttribute, NativeView, NSLayoutAttribute> eq = (v1, a1, v2, a2) => { newConstraints.Add(NSLayoutConstraint.Create(v1, a1, NSLayoutRelation.Equal, v2, a2, 1, 0)); }; Child prev = null; if (lefts.Count > 0) { var first = lefts.First(); eq(nativeView, leftAttr, first.NativeView, leftAttr); prev = first; foreach (var c in lefts.Skip(1)) { eq(prev.NativeView, rightAttr, c.NativeView, leftAttr); prev = c; } } Child subs = null; if (rights.Count > 0) { var last = rights.Last(); eq(nativeView, rightAttr, last.NativeView, rightAttr); subs = last; foreach (var c in rights.Take(rights.Count - 1).Reverse()) { eq(subs.NativeView, leftAttr, c.NativeView, rightAttr); subs = c; } } if (centers.Count > 0) { throw new NotImplementedException("Center alignment not yet supported"); } // // Align the minor axis // foreach (var c in children) { var a = NSLayoutAttribute.Left; if (horizontal) { switch (c.Layout.VerticalAlignment) { case VerticalAlignment.Top: a = NSLayoutAttribute.Top; break; case VerticalAlignment.Center: a = NSLayoutAttribute.CenterY; break; default: a = NSLayoutAttribute.Bottom; break; } } else { switch (c.Layout.HorizontalAlignment) { case HorizontalAlignment.Left: a = NSLayoutAttribute.Left; break; case HorizontalAlignment.Center: a = NSLayoutAttribute.CenterX; break; default: a = NSLayoutAttribute.Right; break; } } eq(nativeView, a, c.NativeView, a); } // Swap out the old, put in the new if (constraints != null) { nativeView.RemoveConstraints(constraints); } constraints = newConstraints.ToArray(); nativeView.AddConstraints(constraints); }
public static void AddConstraints(this UIView view, params FluentLayout[] fluentLayouts) => view.AddConstraints(fluentLayouts.AsEnumerable());