Wenfengcheng / xamarin-notes

xamarin journal
MIT License
3 stars 0 forks source link

xamarin.iOS UIView frame extension #15

Open Wenfengcheng opened 5 years ago

Wenfengcheng commented 5 years ago

Xamarin.iOS UIView frame extension


public static class UIView_extensions
{
/// <summary>
/// Get the x.
/// </summary>
public static nfloat X(this UIView v)
{
return v.Frame.X;
}
    /// <summary>
    /// Sets the x.
    /// </summary>
    public static void SetX(this UIView v, nfloat x)
    {
        v.Frame = new CGRect(x, v.Frame.Y, v.Frame.Width, v.Frame.Height);
    }

    /// <summary>
    /// Get the y.
    /// </summary>
    public static nfloat Y(this UIView v)
    {
        return v.Frame.Y;
    }

    /// <summary>
    /// Sets the y.
    /// </summary>
    public static void SetY(this UIView v, nfloat y)
    {
        v.Frame = new CGRect(v.Frame.X, y, v.Frame.Width, v.Frame.Height);
    }

    /// <summary>
    /// Get the height.
    /// </summary>
    public static nfloat Height(this UIView v)
    {
        return v.Frame.Height;
    }

    /// <summary>
    /// Sets the height.
    /// </summary>
    public static void SetHeight(this UIView v, nfloat h)
    {
        v.SetFrame(v.Frame.X, v.Frame.Y, v.Frame.Width, h);
    }

    /// <summary>
    /// Get the width.
    /// </summary>
    public static nfloat Width(this UIView v)
    {
        return v.Frame.Width;
    }

    /// <summary>
    /// Sets the width.
    /// </summary>
    public static void SetWidth(this UIView v, nfloat w)
    {
        v.SetFrame(v.Frame.X, v.Frame.Y, w, v.Frame.Height);
    }

    /// <summary>
    /// Get the center x.
    /// </summary>
    public static nfloat CenterX(this UIView v)
    {
        return v.Center.X;
    }

    /// <summary>
    /// Sets the center x.
    /// </summary>
    public static void SetCenterX(this UIView v, nfloat centerX)
    {
        v.Center = new CGPoint(centerX, v.Center.Y);
    }

    /// <summary>
    /// Sets the center y.
    /// </summary>
    public static nfloat CenterY(this UIView v)
    {
        return v.Center.Y;
    }

    /// <summary>
    /// Sets the center y.
    /// </summary>
    public static void SetCenterY(this UIView v, nfloat centerY)
    {
        v.Center = new CGPoint(v.Center.Y, centerY);
    }

    /// <summary>
    /// Get the top.
    /// </summary>
    public static nfloat Top(this UIView v)
    {
        return v.Frame.Y;
    }

    /// <summary>
    /// Sets the top.
    /// </summary>
    public static void SetTop(this UIView v, nfloat top)
    {
        v.SetY(top);
    }

    /// <summary>
    /// Get the bottom.
    /// </summary>
    public static nfloat Bottom(this UIView v)
    {
        return v.Frame.Y + v.Frame.Height;
    }

    /// <summary>
    /// Sets the bottom.
    /// </summary>
    public static void SetBottom(this UIView v, nfloat bottom)
    {
        v.SetTop(bottom - v.Frame.Height);
    }

    /// <summary>
    /// Get the location.
    /// </summary>
    public static CGPoint Location(this UIView v)
    {
        return v.Frame.Location;
    }

    /// <summary>
    /// Sets the location.
    /// </summary>
    /// <param name="location">Location.</param>
    public static void SetLocation(this UIView v, CGPoint location)
    {
        v.Frame = new CGRect(location, v.Frame.Size);
    }

    /// <summary>
    /// Sets the location with x and y.
    /// </summary>
    /// <param name="x">The x coordinate.</param>
    /// <param name="y">The y coordinate.</param>
    public static void SetLocation(this UIView v, nfloat x, nfloat y)
    {
        var location = new CGPoint(x, y);
        v.SetLocation(location);
    }

    /// <summary>
    /// Get the size.
    /// </summary>
    /// <returns>The size.</returns>
    public static CGSize Size(this UIView v)
    {
        return v.Frame.Size;
    }

    /// <summary>
    /// Sets the size.
    /// </summary>
    /// <param name="size">Size.</param>
    public static void SetSize(this UIView v, CGSize size)
    {
        v.Frame = new CGRect(v.Frame.Location, size);
    }

    /// <summary>
    /// Sets the size with width and height.
    /// </summary>
    /// <param name="width">Width.</param>
    /// <param name="height">Height.</param>
    public static void SetSize(this UIView v, nfloat width, nfloat height)
    {
        var size = new CGSize(width, height);
        v.SetSize(size);
    }

    /// <summary>
    /// Sets the frame with rect.
    /// </summary>
    /// <param name="rect">Rect.</param>
    public static void SetFrame(this UIView v, CGRect rect)
    {
        v.Frame = rect;
    }

    /// <summary>
    /// Sets the frame with point and size.
    /// </summary>
    /// <param name="point">Point.</param>
    /// <param name="size">Size.</param>
    public static void SetFrame(this UIView v, CGPoint point, CGSize size)
    {
        var rect = new CGRect(point, size);
        v.Frame = rect;
    }

    /// <summary>
    /// Sets the frame with x, y, width and height.
    /// </summary>
    /// <param name="x">The x coordinate.</param>
    /// <param name="y">The y coordinate.</param>
    /// <param name="width">Width.</param>
    /// <param name="height">Height.</param>
    public static void SetFrame(this UIView v, nfloat x, nfloat y, nfloat width, nfloat height)
    {
        var rect = new CGRect(x, y, width, height);
        v.Frame = rect;
    }

}


https://gist.github.com/DarthRamone/9ac0cba4023697fa904d