iOS开发札记iOS Developer

代码实现自动布局

2016-07-15  本文已影响91人  小布走慢点

关于 autoresizingMask 方法

在简单的界面适配时使用 autoresizingMask 及其方便因为是系统的方法直接设置它的属性就行

首先要知道UIViewAutoresizing是一个枚举类型,默认是UIViewAutoresizingNone,也就是不做任何处理。

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone                 = 0,//不会随父视图的改变而改变
UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,//自动调整view与父视图左边距,以保证右边距不变
UIViewAutoresizingFlexibleWidth        = 1 << 1,//自动调整view的宽度,保证左边距和右边距不变
UIViewAutoresizingFlexibleRightMargin  = 1 << 2,//自动调整view与父视图右边距,以保证左边距不变
UIViewAutoresizingFlexibleTopMargin    = 1 << 3,//自动调整view与父视图上边距,以保证下边距不变
UIViewAutoresizingFlexibleHeight       = 1 << 4,//自动调整view的高度,以保证上边距和下边距不变
UIViewAutoresizingFlexibleBottomMargin = 1 << 5//自动调整view与父视图的下边距,以保证上边距不变
};

autoresizing组合使用:

也就是枚举中的值可以使用|隔开,同时拥有多个值的功能,可以针对不同的场景作不同的变化。例如:

// 设置View控件的宽度按照父视图的比例进行缩放,距离父视图顶部、左边距和右边距的距离不变
[View setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];

autolayout 第三方框架Masonry

如果是复杂的界面有各种依赖关系自带方法就不够好用啦,推荐使用 第三方框架Masonry(用的人比较多)

安装:
1.添加Masonry文件夹的所有源代码到项目中
添加2个宏、导入主头文件

 // 只要添加了这个宏,就不用带mas_前缀
#define MAS_SHORTHAND
//只要添加了这个宏,equalTo就等价于mas_equalTo
#define MAS_SHORTHAND_GLOBALS
//这个头文件一定要放在上面两个宏的后面
#import "Masonry.h"

2.使用 pod 'Masonry'

方法

添加约束的方法

mas_make // 这个方法只会添加新的约束
 -> [<view> mas_makeConstraints:^(MASConstraintMaker *make){<code>}];

mas_update // 这个方法将会覆盖以前的某些特定的约束
 -> [<view> mas_updateConstraints:^(MASConstraintMaker *make){<code>}];

mas_remake // 这个方法将会覆盖以前的某些特定的约束
 -> [<view> mas_remakeConstraints:^(MASConstraintMaker *make){<code>}];

Demo:

**居中显示 **

UIView *redView = [[UIView alloc] init]; 
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView]; 
 // 可以使用三个方法来添加约束。
 [redView mas_makeConstraints:^(MASConstraintMaker *make) { 
  make.centerX.equalTo(self.view);
  make.centerY.equalTo(self.view);
  make.height.equalTo(100);
  make.width.equalTo(200);
 }];

并排位于底部,间距20

UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

UIView *blueView= [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];

// 添加约束
[redView makeConstraints:^(MASConstraintMaker *make) {
   make.left.equalTo(self.view.left).offset(20); // 左边间距20
   make.right.equalTo(blueView.left).offset(-20); // 右边和blueView间距20
   make.bottom.equalTo(self.view.bottom).offset(-20); // 底部间距20

   make.height.equalTo(200); // 高度200

}];

  [blueView makeConstraints:^(MASConstraintMaker *make) {
   make.right.equalTo(self.view.right).offset(-20); // 右边间距20
   make.bottom.equalTo(redView.bottom); // 和redView底部间距相同

   make.height.equalTo(redView); // 等宽
   make.width.equalTo(redView); // 等高
}];

四个View,平分整个屏幕

  //红色
  UIView *redView = [[UIView alloc] init];
  redView.backgroundColor = [UIColor redColor];
  [self.view addSubview:redView];
  // 蓝色
  UIView *blueView= [[UIView alloc] init];
  blueView.backgroundColor = [UIColor blueColor];
  [self.view addSubview:blueView];
 // 黑色
 UIView *blackView = [[UIView alloc] init];
 blackView.backgroundColor = [UIColor blackColor];
 [self.view addSubview:blackView];
 // 绿色
 UIView *greenView= [[UIView alloc] init];
 greenView.backgroundColor = [UIColor greenColor];
 [self.view addSubview:greenView];


 // autolayout
 [redView makeConstraints:^(MASConstraintMaker *make) {
     make.left.and.top.equalTo(self.view);
     make.right.equalTo(self.view.centerX);
     make.bottom.equalTo(self.view.centerY);
 }];

 [blueView makeConstraints:^(MASConstraintMaker *make) {
     make.left.equalTo(redView.right);
     make.right.equalTo(self.view);
     make.height.equalTo(redView);

 }];

 [blackView makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(redView.bottom);
     make.height.equalTo(redView);
     make.width.equalTo(redView);
 }];

 [greenView makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(blueView.bottom);
     make.left.equalTo(blackView.right);
     make.height.equalTo(blueView);
     make.width.equalTo(blueView);
 }];

 // 红色View内部
 UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"010.png"]];
 UILabel *name = [[UILabel alloc] init];
 name.text = @"红色";
 name.textAlignment = NSTextAlignmentCenter;
 name.backgroundColor = [UIColor purpleColor];
 [redView addSubview:image];
 [redView addSubview:name];
 [image makeConstraints:^(MASConstraintMaker *make) {
     make.center.equalTo(redView.center).offset(-20);
 }];
 [name makeConstraints:^(MASConstraintMaker *make) {
     make.left.right.equalTo(redView.left);
     make.bottom.equalTo(redView.bottom);
     make.height.equalTo(40);
 }];

参考
自动布局之autoresizingMask使用详解(Storyboard&Code)
Masonry
ios开发学习笔记040-autolayout 第三方框架Masonry

上一篇 下一篇

猜你喜欢

热点阅读