IOS不用AutoLayout也能实现自动布局的类(2)----MyFrameLayout 框架布局

我们知道,在IOS中我们要想进行视图的各方向的停靠,需要用到autoresizingMask,以及不停的计算应该停靠的位置,也就是计算frame中的x,y,width,height,这样大量的编码导致计算繁琐而且容易出错,因此我这里推出了另外新的布局模式MyFrameLayout。这个布局可以让子视图实现左中右,上中下,填充等功能的布局,同时还可以设置停靠的布局的位置的边距,我们对子视图扩展出了停靠位置的属性:

 

@property(nonatomic,assign)MarignGravity marginGravity;

 

这个属性用来控制子视图停靠在MyFrameLayout的方位,这些方位可以是如下方位的或组合:

 

    MGRAVITY_NONE  //不采用停靠模式

    MGRAVITY_HORZ_LEFT  //水平居左

    MGRAVITY_HORZ_CENTER  //水平居中

    MGRAVITY_HORZ_RIGHT  //水平居右

    MGRAVITY_HORZ_FILL  //水平填充整个布局,视图会有拉伸

    MGRAVITY_VERT_TOP  //垂直居上

    MGRAVITY_VERT_CENTER //垂直居中

    MGRAVITY_VERT_BOTTOM  //垂直居下

     MGRAVITY_VERT_FILL  //垂直填充整个布局,视图会有拉伸

    MGRAVITY_CENTER  //整个视图居中

    MGRAVITY_FILL  //整个视图填满布局视图

 

除了可以让子视图停靠在布局的方位外,还可以指定子视图离停靠位置的边距,这个可以通过扩展的视图的四个属性:

 

@property(nonatomic,assign)CGFloat topMargin;

@property(nonatomic,assign)CGFloat leftMargin;

@property(nonatomic,assign)CGFloat bottomMargin;

@property(nonatomic,assign)CGFloat rightMargin;

 

这四个属性用来设置视图离停靠位置的四个距离。同时MyFrameLayout中还提供一个padding的属性用来控制整体的子视图离自己的边距。

因为这个布局的使用比较简单,下面直接可以看图:

 

 

对应的代码如下:

 

 

  1. MyFrameLayout *fl = [[MyFrameLayout alloc] initWithFrame:self.view.bounds];  
  2.   fl.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  
  3.   fl.padding = UIEdgeInsetsMake(20, 20, 20, 20);  
  4.   fl.backgroundColor = [UIColor grayColor];  
  5.     
  6.   //显示全屏  
  7.   UILabel *fill = UILabel.new;  
  8.   fill.text = @"                fill";  
  9.  // fill.textAlignment = NSTextAlignmentCenter;  
  10.   fill.backgroundColor = [UIColor blueColor];  
  11.   fill.marginGravity = MGRAVITY_FILL;  
  12.   [fl addSubview:fill];  
  13.     
  14.     
  15.     
  16.   //左右填充。  
  17.   UILabel *horzFill = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 120)];  
  18.   horzFill.text = @"Horz Fill";  
  19.   horzFill.textAlignment = NSTextAlignmentCenter;  
  20.   horzFill.backgroundColor = [UIColor greenColor];  
  21.   horzFill.marginGravity = MGRAVITY_HORZ_FILL;  
  22.   [fl addSubview:horzFill];  
  23.     
  24.     
  25.   //左右居中  
  26.   UILabel *horzCenter = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];  
  27.   horzCenter.text = @"Horz Center";  
  28.   horzCenter.backgroundColor = [UIColor whiteColor];  
  29.   horzCenter.marginGravity = MGRAVITY_HORZ_CENTER;  
  30.   [fl addSubview:horzCenter];  
  31.   
  32.     
  33.   //左上  
  34.   UILabel *topLeft = UILabel.new;  
  35.   topLeft.text = @"topLeft";  
  36.   [topLeft sizeToFit];  
  37.   topLeft.backgroundColor = [UIColor whiteColor];  
  38.   topLeft.marginGravity = MGRAVITY_HORZ_LEFT | MGRAVITY_VERT_TOP;  
  39.   [fl addSubview:topLeft];  
  40.     
  41.   //左中  
  42.   UILabel *centerLeft = UILabel.new;  
  43.   centerLeft.text = @"centerLeft";  
  44.   [centerLeft sizeToFit];  
  45.   centerLeft.backgroundColor = [UIColor whiteColor];  
  46.   centerLeft.marginGravity = MGRAVITY_HORZ_LEFT | MGRAVITY_VERT_CENTER;  
  47.   [fl addSubview:centerLeft];  
  48.     
  49.     
  50.   //左下  
  51.   UILabel *bottomLeft = UILabel.new;  
  52.   bottomLeft.text = @"bottomLeft";  
  53.   [bottomLeft sizeToFit];  
  54.   bottomLeft.backgroundColor = [UIColor whiteColor];  
  55.   bottomLeft.marginGravity = MGRAVITY_HORZ_LEFT | MGRAVITY_VERT_BOTTOM;  
  56.   [fl addSubview:bottomLeft];  
  57.     
  58.     
  59.   //中上  
  60.   UILabel *topCenter = UILabel.new;  
  61.   topCenter.text = @"topCenter";  
  62.   [topCenter sizeToFit];  
  63.   topCenter.backgroundColor = [UIColor greenColor];  
  64.   topCenter.marginGravity = MGRAVITY_HORZ_CENTER | MGRAVITY_VERT_TOP;  
  65.   [fl addSubview:topCenter];  
  66.     
  67.     
  68.   //中中。  
  69.   UILabel *centerCenter = UILabel.new;  
  70.   centerCenter.text = @"centerCenter";  
  71.   [centerCenter sizeToFit];  
  72.   centerCenter.backgroundColor = [UIColor greenColor];  
  73.   centerCenter.marginGravity = MGRAVITY_HORZ_CENTER | MGRAVITY_VERT_CENTER;  
  74.   [fl addSubview:centerCenter];  
  75.     
  76.     
  77.   //中下  
  78.   UILabel *bottomCenter = UILabel.new;  
  79.   bottomCenter.text = @"bottomCenter";  
  80.   [bottomCenter sizeToFit];  
  81.   bottomCenter.backgroundColor = [UIColor greenColor];  
  82.   bottomCenter.marginGravity = MGRAVITY_HORZ_CENTER | MGRAVITY_VERT_BOTTOM;  
  83.   [fl addSubview:bottomCenter];  
  84.   
  85.     
  86.   //右上  
  87.   UILabel *topRight = UILabel.new;  
  88.   topRight.text = @"topRight";  
  89.   [topRight sizeToFit];  
  90.   topRight.backgroundColor = [UIColor greenColor];  
  91.   topRight.marginGravity = MGRAVITY_HORZ_RIGHT | MGRAVITY_VERT_TOP;  
  92.   [fl addSubview:topRight];  
  93.     
  94.     
  95.   //右中  
  96.   UILabel *centerRight = UILabel.new;  
  97.   centerRight.text = @"centerRight";  
  98.   [centerRight sizeToFit];  
  99.   centerRight.backgroundColor = [UIColor greenColor];  
  100.   centerRight.marginGravity = MGRAVITY_HORZ_RIGHT | MGRAVITY_VERT_CENTER;  
  101.   [fl addSubview:centerRight];  
  102.   
  103.     
  104.   UILabel *bottomRight = UILabel.new;  
  105.   bottomRight.text = @"bottomRight";  
  106.   [bottomRight sizeToFit];  
  107.   bottomRight.backgroundColor = [UIColor greenColor];  
  108.   bottomRight.marginGravity = MGRAVITY_HORZ_RIGHT | MGRAVITY_VERT_BOTTOM;  
  109.   [fl addSubview:bottomRight];  
  110.     
  111.   
  112.   //居中显示。  
  113.   UILabel *center = UILabel.new;  
  114.   center.text = @"center";  
  115.   [center sizeToFit];  
  116.   center.backgroundColor = [UIColor redColor];  
  117.   center.marginGravity = MGRAVITY_CENTER;  
  118.   center.leftMargin = 30;  
  119.   center.rightMargin = 30;  
  120.   center.topMargin = 30;  
  121.   center.bottomMargin = 30;  
  122.   [fl addSubview:center];  
  123.   
  124.     
  125.   [self.view addSubview:fl];  

 从代码中我们可以看到每个视图只需要设置marginGravity的对应的停靠的位置,以及设置对应的xxxMargin边距,还有设置MyFrameLayout的padding值来设置里面里面的子视图离自己的边距。

 

 

总结:

   对于那些希望固定在某个位置的子视图来说,我们可以通过将视图加入到MyFrameLayout中来实现。

posted on 2015-09-14 16:04  廖利君  阅读(167)  评论(0编辑  收藏  举报