/// <summary>
        /// Sets the left constraint of the current view to the right of the given view.
        /// Note: The view given has to have been attached using the Attach method.
        /// Any margin of the given view and the current view will be added (no margin collapse).
        /// </summary>
        /// <returns>The of.</returns>
        /// <param name="view">View.</param>
        /// <param name="margin">Margin.</param>
        /// <param name="priority">Constraint priority.</param>
        public ConstraintHelper LeftOf(UIView view, float?margin = null, float?priority = null)
        {
            ConstraintContainer item = _items.FirstOrDefault(items => (items.View == view));

            if (item == null)
            {
                throw new UnattachedViewException();
            }
            return(LeftOf(item, margin, priority));
        }
        /// <summary>
        /// Sets the height constraint of the current view to the height of the given view.
        /// Note: The view given has to have been attached using the Attach method.
        /// You can optionally multiply the views height and/or modify it.
        /// </summary>
        /// <returns>The of.</returns>
        /// <param name="view">View.</param>
        /// <param name="multiplier">Multiplier.</param>
        /// <param name="modifier">Modifier.</param>
        /// <param name="priority">Constraint priority.</param>
        public ConstraintHelper HeightOf(UIView view, float multiplier = 1, float modifier = 0, float?priority = null)
        {
            ConstraintContainer item = _items.FirstOrDefault(items => (items.View == view));

            if (item == null)
            {
                throw new UnattachedViewException();
            }
            return(HeightOf(item, multiplier, modifier, priority));
        }
 /// <summary>
 /// Sets the current view to work with to the given one.
 /// Note: The view has to have been attached previously using the 'Attach' method.
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 /// <param name="view">View.</param>
 public ConstraintHelper WorkWith(UIView view)
 {
     if (_currentItem != null && _currentItem.View == view)
     {
         return(this);
     }
     _currentItem = _items.FirstOrDefault(items => (items.View == view));
     if (_currentItem == null)
     {
         throw new UnattachedViewException();
     }
     return(this);
 }
 /// <summary>
 /// Sets the bottom constraint of the current view to the bottom of the view within the given constraint container.
 /// Any margin of the given view and the current view will be added (no margin collapse).
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 /// <param name="container">Container.</param>
 /// <param name="margin">Margin.</param>
 public ConstraintHelper AboveOf(ConstraintContainer container, float? margin = null)
 {
     if (margin != null) {
     _currentItem.Margin.Bottom = (float)margin;
     }
     _currentItem.ConstraintBottom = NSLayoutConstraint.Create(
         _currentItem.View, NSLayoutAttribute.Bottom,
         NSLayoutRelation.Equal,
         container.View, NSLayoutAttribute.Top,
     1f, 0 - (_currentItem.Margin.Bottom + container.Margin.Top)
     );
     _view.AddConstraint(_currentItem.ConstraintBottom);
     return this;
 }
 /// <summary>
 /// Adds the given view as a subview and the given view as current view to work with
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 /// <param name="view">View.</param>
 public ConstraintHelper Attach(UIView view)
 {
     _currentItem = _items.FirstOrDefault(items => (items.View == view));
     if (_currentItem == null) {
         view.TranslatesAutoresizingMaskIntoConstraints = false;
         _currentItem = new ConstraintContainer(view);
     if(view is IConstraintHelpedView) {
       _currentItem.Margin = ((IConstraintHelpedView)view).Margin;
     }
         _view.AddSubview(view);
     }
     _items.Add(_currentItem);
     return this;
 }
 /// <summary>
 /// Sets the height constraint of the current view to the height of the given view.
 /// Note: The view given has to have been attached using the Attach method.
 /// You can optionally multiply the views height and/or modify it.
 /// </summary>
 /// <returns>The of.</returns>
 /// <param name="container">Container.</param>
 /// <param name="multiplier">Multiplier.</param>
 /// <param name="modifier">Modifier.</param>
 /// <param name="priority">Constraint priority.</param>
 public ConstraintHelper HeightOf(ConstraintContainer container, float multiplier = 1, float modifier = 0, float?priority = null)
 {
     _currentItem.ConstraintHeight = NSLayoutConstraint.Create(
         _currentItem.View, NSLayoutAttribute.Height,
         NSLayoutRelation.Equal,
         container.View, NSLayoutAttribute.Height,
         multiplier, modifier
         );
     if (priority != null)
     {
         _currentItem.ConstraintHeight.Priority = (float)priority;
     }
     _view.AddConstraint(_currentItem.ConstraintHeight);
     return(this);
 }
 /// <summary>
 /// Removes all views and related constraints added through the ConstraintHelper object.
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 public ConstraintHelper RemoveAll()
 {
     foreach (var item in _items)
     {
         var constraints = item.GetAllConstraints();
         if (constraints.Count > 0)
         {
             _view.RemoveConstraints(constraints.ToArray());
             item.EmptyConstraints();
         }
         item.View.RemoveFromSuperview();
     }
     _items       = new List <ConstraintContainer>();
     _currentItem = null;
     return(this);
 }
 /// <summary>
 /// Adds the given view as a subview and the given view as current view to work with
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 /// <param name="view">View.</param>
 public ConstraintHelper Attach(UIView view)
 {
     _currentItem = _items.FirstOrDefault(items => (items.View == view));
     if (_currentItem == null)
     {
         view.TranslatesAutoresizingMaskIntoConstraints = false;
         _currentItem = new ConstraintContainer(view);
         if (view is IConstraintHelpedView)
         {
             _currentItem.Margin = ((IConstraintHelpedView)view).Margin;
         }
         _view.AddSubview(view);
     }
     _items.Add(_currentItem);
     return(this);
 }
        /// <summary>
        /// Removes the currently selected view and all related constraints from the parent view.
        /// </summary>
        /// <returns>ConstraintHelper</returns>
        public ConstraintHelper Remove()
        {
            if (_currentItem == null)
            {
                throw new UnattachedViewException();
            }
            var constraints = _currentItem.GetAllConstraints();

            if (constraints.Count > 0)
            {
                _view.RemoveConstraints(constraints.ToArray());
                _currentItem.EmptyConstraints();
            }
            _currentItem.View.RemoveFromSuperview();
            _items.Remove(_currentItem);
            _currentItem = null;
            return(this);
        }
 /// <summary>
 /// Sets the bottom constraint of the current view to the bottom of the view within the given constraint container.
 /// Any margin of the given view and the current view will be added (no margin collapse).
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 /// <param name="container">Container.</param>
 /// <param name="margin">Margin.</param>
 /// <param name="priority">Constraint priority.</param>
 public ConstraintHelper AboveOf(ConstraintContainer container, float?margin = null, float?priority = null)
 {
     if (margin != null)
     {
         _currentItem.Margin.Bottom = (float)margin;
     }
     _currentItem.ConstraintBottom = NSLayoutConstraint.Create(
         _currentItem.View, NSLayoutAttribute.Bottom,
         NSLayoutRelation.Equal,
         container.View, NSLayoutAttribute.Top,
         1f, 0 - (_currentItem.Margin.Bottom + container.Margin.Top)
         );
     if (priority != null)
     {
         _currentItem.ConstraintBottom.Priority = (float)priority;
     }
     _view.AddConstraint(_currentItem.ConstraintBottom);
     return(this);
 }
 /// <summary>
 /// Sets the left constraint of the current view to the right of the view within the given constraint container.
 /// Any margin of the given view and the current view will be added (no margin collapse).
 /// </summary>
 /// <returns>The of.</returns>
 /// <param name="container">Container.</param>
 /// <param name="margin">Margin.</param>
 /// <param name="priority">Constraint priority.</param>
 public ConstraintHelper LeftOf(ConstraintContainer container, float?margin = null, float?priority = null)
 {
     if (margin != null)
     {
         _currentItem.Margin.Left = (float)margin;
     }
     _currentItem.ConstraintLeft = NSLayoutConstraint.Create(
         _currentItem.View, NSLayoutAttribute.Left,
         NSLayoutRelation.Equal,
         container.View, NSLayoutAttribute.Right,
         1f, _currentItem.Margin.Left + container.Margin.Right
         );
     if (priority != null)
     {
         _currentItem.ConstraintLeft.Priority = (float)priority;
     }
     _view.AddConstraint(_currentItem.ConstraintLeft);
     return(this);
 }
 /// <summary>
 /// Resets the top stack.
 /// </summary>
 /// <returns>The top stack.</returns>
 public ConstraintHelper ResetTopStack()
 {
     _lastTop = null;
     return this;
 }
 /// <summary>
 /// Resets the left stack.
 /// </summary>
 /// <returns>The left stack.</returns>
 public ConstraintHelper ResetLeftStack()
 {
     _lastLeft = null;
     return(this);
 }
 /// <summary>
 /// Resets the bottom stack.
 /// </summary>
 /// <returns>The bottom stack.</returns>
 public ConstraintHelper ResetBottomStack()
 {
     _lastBottom = null;
     return(this);
 }
 /// <summary>
 /// Resets the right stack.
 /// </summary>
 /// <returns>The right stack.</returns>
 public ConstraintHelper ResetRightStack()
 {
     _lastRight = null;
     return(this);
 }
 /// <summary>
 /// Sets the current view to be the last view in the right stack.
 /// Every following right constraint will be right of this current view.
 /// </summary>
 /// <returns>The top.</returns>
 public ConstraintHelper StackRight()
 {
     _lastRight = _currentItem;
     return this;
 }
 /// <summary>
 /// Resets the bottom stack.
 /// </summary>
 /// <returns>The bottom stack.</returns>
 public ConstraintHelper ResetBottomStack()
 {
     _lastBottom = null;
     return this;
 }
 /// <summary>
 /// Removes all views and related constraints added through the ConstraintHelper object.
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 public ConstraintHelper RemoveAll()
 {
     foreach (var item in _items) {
         var constraints = item.GetAllConstraints();
         if (constraints.Count > 0) {
             _view.RemoveConstraints(constraints.ToArray());
             item.EmptyConstraints();
         }
         item.View.RemoveFromSuperview();
     }
     _items = new List<ConstraintContainer>();
     _currentItem = null;
     return this;
 }
 /// <summary>
 /// Sets the right constraint of the current view to the left of the view within the given constraint container.
 /// Any margin of the given view and the current view will be added (no margin collapse).
 /// </summary>
 /// <returns>The of.</returns>
 /// <param name="container">Container.</param>
 /// <param name="margin">Margin.</param>
 public ConstraintHelper RightOf(ConstraintContainer container, float? margin = null)
 {
     if (margin != null) {
     _currentItem.Margin.Right = (float)margin;
     }
     _currentItem.ConstraintRight = NSLayoutConstraint.Create(
         _currentItem.View, NSLayoutAttribute.Right,
         NSLayoutRelation.Equal,
         container.View, NSLayoutAttribute.Left,
     1f, 0 - (_currentItem.Margin.Right + container.Margin.Left)
     );
     _view.AddConstraint(_currentItem.ConstraintRight);
     return this;
 }
 /// <summary>
 /// Sets the current view to work with to the given one.
 /// Note: The view has to have been attached previously using the 'Attach' method.
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 /// <param name="view">View.</param>
 public ConstraintHelper WorkWith(UIView view)
 {
     if (_currentItem != null && _currentItem.View == view) { return this; }
     _currentItem = _items.FirstOrDefault(items => (items.View == view));
     if (_currentItem == null) { throw new UnattachedViewException(); }
     return this;
 }
 /// <summary>
 /// Sets the current view to be the last view in the bottom stack.
 /// Every following bottom constraint will be above of this current view.
 /// </summary>
 /// <returns>The bottom.</returns>
 public ConstraintHelper StackBottom()
 {
     _lastBottom = _currentItem;
     return this;
 }
 /// <summary>
 /// Sets the width constraint of the current view to the width of the given view.
 /// Note: The view given has to have been attached using the Attach method.
 /// You can optionally multiply the views width and/or modify it.
 /// </summary>
 /// <returns>The of.</returns>
 /// <param name="container">Container.</param>
 /// <param name="multiplier">Multiplier.</param>
 /// <param name="modifier">Modifier.</param>
 public ConstraintHelper WidthOf(ConstraintContainer container, float multiplier = 1, float modifier = 0)
 {
     _currentItem.ConstraintWidth = NSLayoutConstraint.Create(
         _currentItem.View, NSLayoutAttribute.Width,
         NSLayoutRelation.Equal,
         container.View, NSLayoutAttribute.Width,
         multiplier, modifier
     );
     _view.AddConstraint(_currentItem.ConstraintWidth);
     return this;
 }
 /// <summary>
 /// Sets the current view to be the last view in the top stack.
 /// Every following top constraint will be below this current view.
 /// </summary>
 /// <returns>The top.</returns>
 public ConstraintHelper StackTop()
 {
     _lastTop = _currentItem;
     return this;
 }
 /// <summary>
 /// Sets the current view to be the last view in the bottom stack.
 /// Every following bottom constraint will be above of this current view.
 /// </summary>
 /// <returns>The bottom.</returns>
 public ConstraintHelper StackBottom()
 {
     _lastBottom = _currentItem;
     return(this);
 }
 /// <summary>
 /// Resets the right stack.
 /// </summary>
 /// <returns>The right stack.</returns>
 public ConstraintHelper ResetRightStack()
 {
     _lastRight = null;
     return this;
 }
 /// <summary>
 /// Sets the current view to be the last view in the left stack.
 /// Every following left constraint will be left of this current view.
 /// </summary>
 /// <returns>The left.</returns>
 public ConstraintHelper StackLeft()
 {
     _lastLeft = _currentItem;
     return(this);
 }
 /// <summary>
 /// Resets the left stack.
 /// </summary>
 /// <returns>The left stack.</returns>
 public ConstraintHelper ResetLeftStack()
 {
     _lastLeft = null;
     return this;
 }
 /// <summary>
 /// Resets the top stack.
 /// </summary>
 /// <returns>The top stack.</returns>
 public ConstraintHelper ResetTopStack()
 {
     _lastTop = null;
     return(this);
 }
 /// <summary>
 /// Sets the current view to be the last view in the top stack.
 /// Every following top constraint will be below this current view.
 /// </summary>
 /// <returns>The top.</returns>
 public ConstraintHelper StackTop()
 {
     _lastTop = _currentItem;
     return(this);
 }
 /// <summary>
 /// Sets the current view to be the last view in the left stack.
 /// Every following left constraint will be left of this current view.
 /// </summary>
 /// <returns>The left.</returns>
 public ConstraintHelper StackLeft()
 {
     _lastLeft = _currentItem;
     return this;
 }
 /// <summary>
 /// Sets the current view to be the last view in the right stack.
 /// Every following right constraint will be right of this current view.
 /// </summary>
 /// <returns>The top.</returns>
 public ConstraintHelper StackRight()
 {
     _lastRight = _currentItem;
     return(this);
 }
 /// <summary>
 /// Removes the currently selected view and all related constraints from the parent view.
 /// </summary>
 /// <returns>ConstraintHelper</returns>
 public ConstraintHelper Remove()
 {
     if (_currentItem == null) { throw new UnattachedViewException(); }
     var constraints = _currentItem.GetAllConstraints();
     if(constraints.Count > 0) {
         _view.RemoveConstraints(constraints.ToArray());
         _currentItem.EmptyConstraints();
     }
     _currentItem.View.RemoveFromSuperview();
     _items.Remove(_currentItem);
     _currentItem = null;
     return this;
 }