下载地址:
https://github.com/youngsoft/MyLinearLayout
我们知道,在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的属性用来控制整体的子视图离自己的边距。
因为这个布局的使用比较简单,下面直接可以看图:
对应的代码如下:
[objc] view plaincopy
MyFrameLayout *fl = [[MyFrameLayout alloc] initWithFrame:self.view.bounds];
fl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
fl.padding = UIEdgeInsetsMake(20, 20, 20, 20);
fl.backgroundColor = [UIColor grayColor];
//显示全屏
UILabel *fill = UILabel.new;
fill.text = @" fill";
// fill.textAlignment = NSTextAlignmentCenter;
fill.backgroundColor = [UIColor blueColor];
fill.marginGravity = MGRAVITY_FILL;
[fl addSubview:fill];
//左右填充。
UILabel *horzFill = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 120)];
horzFill.text = @"Horz Fill";
horzFill.textAlignment = NSTextAlignmentCenter;
horzFill.backgroundColor = [UIColor greenColor];
horzFill.marginGravity = MGRAVITY_HORZ_FILL;
[fl addSubview:horzFill];
//左右居中
UILabel *horzCenter = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
horzCenter.text = @"Horz Center";
horzCenter.backgroundColor = [UIColor whiteColor];
horzCenter.marginGravity = MGRAVITY_HORZ_CENTER;
[fl addSubview:horzCenter];
//左上
UILabel *topLeft = UILabel.new;
topLeft.text = @"topLeft";
[topLeft sizeToFit];
topLeft.backgroundColor = [UIColor whiteColor];
topLeft.marginGravity = MGRAVITY_HORZ_LEFT | MGRAVITY_VERT_TOP;
[fl addSubview:topLeft];
//左中
UILabel *centerLeft = UILabel.new;
centerLeft.text = @"centerLeft";
[centerLeft sizeToFit];
centerLeft.backgroundColor = [UIColor whiteColor];
centerLeft.marginGravity = MGRAVITY_HORZ_LEFT | MGRAVITY_VERT_CENTER;
[fl addSubview:centerLeft];
//左下
UILabel *bottomLeft = UILabel.new;
bottomLeft.text = @"bottomLeft";
[bottomLeft sizeToFit];
bottomLeft.backgroundColor = [UIColor whiteColor];
bottomLeft.marginGravity = MGRAVITY_HORZ_LEFT | MGRAVITY_VERT_BOTTOM;
[fl addSubview:bottomLeft];
//中上
UILabel *topCenter = UILabel.new;
topCenter.text = @"topCenter";
[topCenter sizeToFit];
topCenter.backgroundColor = [UIColor greenColor];
topCenter.marginGravity = MGRAVITY_HORZ_CENTER | MGRAVITY_VERT_TOP;
[fl addSubview:topCenter];
//中中。
UILabel *centerCenter = UILabel.new;
centerCenter.text = @"centerCenter";
[centerCenter sizeToFit];
centerCenter.backgroundColor = [UIColor greenColor];
centerCenter.marginGravity = MGRAVITY_HORZ_CENTER | MGRAVITY_VERT_CENTER;
[fl addSubview:centerCenter];
//中下
UILabel *bottomCenter = UILabel.new;
bottomCenter.text = @"bottomCenter";
[bottomCenter sizeToFit];
bottomCenter.backgroundColor = [UIColor greenColor];
bottomCenter.marginGravity = MGRAVITY_HORZ_CENTER | MGRAVITY_VERT_BOTTOM;
[fl addSubview:bottomCenter];
//右上
UILabel *topRight = UILabel.new;
topRight.text = @"topRight";
[topRight sizeToFit];
topRight.backgroundColor = [UIColor greenColor];
topRight.marginGravity = MGRAVITY_HORZ_RIGHT | MGRAVITY_VERT_TOP;
[fl addSubview:topRight];
//右中
UILabel *centerRight = UILabel.new;
centerRight.text = @"centerRight";
[centerRight sizeToFit];
centerRight.backgroundColor = [UIColor greenColor];
centerRight.marginGravity = MGRAVITY_HORZ_RIGHT | MGRAVITY_VERT_CENTER;
[fl addSubview:centerRight];
UILabel *bottomRight = UILabel.new;
bottomRight.text = @"bottomRight";
[bottomRight sizeToFit];
bottomRight.backgroundColor = [UIColor greenColor];
bottomRight.marginGravity = MGRAVITY_HORZ_RIGHT | MGRAVITY_VERT_BOTTOM;
[fl addSubview:bottomRight];
//居中显示。
UILabel *center = UILabel.new;
center.text = @"center";
[center sizeToFit];
center.backgroundColor = [UIColor redColor];
center.marginGravity = MGRAVITY_CENTER;
center.leftMargin = 30;
center.rightMargin = 30;
center.topMargin = 30;
center.bottomMargin = 30;
[fl addSubview:center];
[self.view addSubview:fl];
从代码中我们可以看到每个视图只需要设置marginGravity的对应的停靠的位置,以及设置对应的xxxMargin边距,还有设置MyFrameLayout的padding值来设置里面里面的子视图离自己的边距。
总结:
对于那些希望固定在某个位置的子视图来说,我们可以通过将视图加入到MyFrameLayout中来实现。
https://github.com/youngsoft/MyLinearLayout