Example #1
0
        /// <summary>
        /// Anchors the view to its parent, the parent is considered a container meaning the view will be anchored on the inside edges of its parent.
        /// For instance, a constant value of 5 from the right edge will anchor the child to remain 5 spaces to the left of the parent's right edge to remain inside the parent.
        /// </summary>
        /// <param name="view">The view to anchor.</param>
        /// <param name="edges">The edges to anchor.</param>
        /// <param name="constant">A constant identifying the edge margins when anchoring to the parent.</param>
        /// <returns>The NSLayoutConstraints for the anchor.</returns>
        public static NSLayoutConstraint[] Anchor(this UIView view, AnchorEdges edges = AnchorEdges.All, float constant = 0)
        {
            UIViewExtensions.EnsureHasParent(view);

            List <NSLayoutConstraint> constraints = new List <NSLayoutConstraint>();

            if ((edges & AnchorEdges.Top) == AnchorEdges.Top)
            {
                constraints.Add(UIViewExtensions.Activate(view.TopAnchor.ConstraintEqualTo(view.Superview.TopAnchor, constant)));
            }

            if ((edges & AnchorEdges.Bottom) == AnchorEdges.Bottom)
            {
                constraints.Add(UIViewExtensions.Activate(view.BottomAnchor.ConstraintEqualTo(view.Superview.BottomAnchor, -constant)));
            }

            if ((edges & AnchorEdges.Left) == AnchorEdges.Left)
            {
                constraints.Add(UIViewExtensions.Activate(view.LeftAnchor.ConstraintEqualTo(view.Superview.LeftAnchor, constant)));
            }

            if ((edges & AnchorEdges.Right) == AnchorEdges.Right)
            {
                constraints.Add(UIViewExtensions.Activate(view.RightAnchor.ConstraintEqualTo(view.Superview.RightAnchor, -constant)));
            }

            return(constraints.ToArray());
        }
Example #2
0
 /// <summary>
 /// Centers the view horizontally to the parent.
 /// </summary>
 /// <param name="view">A view to center.</param>
 /// <param name="constant">An optional offset value when centering.</param>
 /// <returns>The NSLayoutConstraint for the anchor.</returns>
 public static NSLayoutConstraint AnchorCenterX(this UIView view, float constant = 0)
 {
     UIViewExtensions.EnsureHasParent(view);
     return(view.AnchorCenterX(view.Superview, constant));
 }
Example #3
0
 /// <summary>
 /// Centers the view both vertically and horizontally to its parent.
 /// </summary>
 /// <param name="view">A view to center.</param>
 /// <returns>The NSLayoutConstraints for the anchor.</returns>
 public static NSLayoutConstraint[] AnchorCenter(this UIView view)
 {
     UIViewExtensions.EnsureHasParent(view);
     return(view.AnchorCenter(view.Superview));
 }
Example #4
0
 /// <summary>
 /// Centers the view vertically to the center of another view.
 /// </summary>
 /// <param name="view">A view to center.</param>
 /// <param name="secondView">The second view to center to.</param>
 /// <param name="constant">An optional offset value when centering.</param>
 /// <returns>The NSLayoutConstraint for the anchor.</returns>
 public static NSLayoutConstraint AnchorCenterY(this UIView view, UIView secondView, float constant = 0)
 {
     UIViewExtensions.EnsureHasParent(view);
     return(UIViewExtensions.Activate(view.CenterYAnchor.ConstraintEqualTo(secondView.CenterYAnchor, constant)));
 }