借鉴:
Masonry下载地址:
https://github.com/SnapKit/Masonry
本文Demo下载地址:
https://github.com/saitjr/MasonryDemo.git
//
// ViewController.m
// Masonry——横屏适配##
##
//
// Created by lanou3g on 15/9/7.
// Copyright (c) 2015年 李富涛. All rights reserved.
//
import “ViewController.h”
import “Masonry.h”
@interface ViewController ()
@end
/**
*
1、可以给控件添加left/right/top/bottom/size/height/width/insert约束;
2、库提供了三个方法,mas_makeConstraints添加约束,mas_updateConstraints修改约束,mas_remakeConstraints清除以前约束并添加新约束;
3、可以通过view.mas_bottom获得view的某个约束;
4、在约束的block中,使用make来给当前控件添加约束。
*/
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];// 防止block中的循环引用
__weak typeof(self) weakSelf = self;
// 初始化view并设置背景
UIView *view = [UIView new];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];// 使用 mas_makeConstraints 添加约束
[view mas_makeConstraints:^(MASConstraintMaker *make) {// 添加大小约束(make就是要添加约束的控件view)
// make.size.mas_equalTo(CGSizeMake(100, 200));
// make.center.equalTo(weakSelf.view); // 添加居中约束(居中方式与self相同)
make.left.and.top.equalTo(weakSelf).with.offset(10); // 距离左边和上边 10
make.right.and.bottom.equalTo(weakSelf).with.offset(-20); // 距离下边和右边 20
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end