Xamarin UIViewの位置大きさのExtension

  • 0
    Like
  • 0
    Comment

    Xamarin UIViewの位置大きさのExtension

    UIViewを継承しているUIキットのコントローラーに対して有効です。

    btn = new UIButton();
    
    btn.X();
    btn.Y();
    btn.Width();
    btn.Height();
    
    btn.X(50);
    btn.Y(50);
    btn.Width(100);
    btn.Height(100);
    

    FrameやBoundsを使わなくても値の取得や設定ができます。

    using System;
    using UIKit;
    using Foundation;
    using CoreGraphics;
    
    namespace test{
    
        public static partial class UIView_Extensions{        
    
            public static int X(this UIView view){
                return Convert.ToInt32((nfloat)view.Frame.X);
            }
    
            public static void X(this UIView view,int x){
                CGRect frame = view.Frame;
                frame.X = x;
                view.Frame = frame;
            }
    
            public static int Y(this UIView view){
                return  Convert.ToInt32((nfloat)view.Frame.Y);
            }
    
            public static void Y(this UIView view,int y){
                CGRect frame = view.Frame;
               frame.Y = y;
                view.Frame = frame;
            }
    
            public static int CenterX(this UIView view){
                return Convert.ToInt32((nfloat)view.Center.X);
            }      
    
            public static void CenterX(this UIView view,int x){
                view.Center = new CGPoint(x, view.Center.Y);
            }
    
            public static int CenterY(this UIView view){
                return Convert.ToInt32((nfloat)view.Center.Y);
            }      
    
            public static void CenterY(this UIView view,int y){
                view.Center = new CGPoint(y, view.Center.Y);
            }          
    
            public static int Width(this UIView view){
                return Convert.ToInt32(view.Frame.Width);
            }      
    
            public static void Width(this UIView view,int width){
                CGRect frame = view.Frame;
                frame.Width = width;
                view.Frame = frame;
            }  
            public static int Height(this UIView view){
                return  Convert.ToInt32(view.Frame.Height);
            }      
    
            public static void Height(this UIView view,int height){
                CGRect frame = view.Frame;
                frame.Height = height;
                view.Frame = frame;
            } 
    
            public static int Top(this UIView view){
                return Convert.ToInt32(view.Frame.Y);
            }     
    
            public static void Top(this UIView view,int top){
                CGRect frame = view.Frame;
                frame.Y = top;
                view.Frame = frame;
            }
    
            public static int Left(this UIView view){
                return Convert.ToInt32(view.Frame.X);
            }     
    
            public static void Left(this UIView view,int left){
                CGRect frame = view.Frame;
                frame.X = left;
                view.Frame = frame;
            }
    
            public static int Right(this UIView view){
                return Convert.ToInt32(view.Frame.X + view.Frame.Width);
            }     
    
            public static void Right(this UIView view,int right){
                CGRect frame = view.Frame;
               frame.X = right - frame.Width;
                view.Frame = frame;
            } 
    
            public static int Bottom(this UIView view){
                return Convert.ToInt32(view.Frame.Y + view.Frame.Height);
            }     
    
            public static void Bottom(this UIView view,int bottom){
                CGRect frame = view.Frame;
                frame.Y = bottom - frame.Height;
                view.Frame = frame;
            }  
    
            public static CGSize Size(this UIView view){
                return view.Frame.Size;
            }
    
            public static void Size(this UIView view,CGSize size){
                CGRect frame = view.Frame;
                frame.Size = size;
                view.Frame = frame;
            } 
        }
    }
    

    JetBrainsのIDE(Rider EAP)がXamarinに対応したみたいなので試してみた