首先說幾點:
- 我一般將數值類型的約束用
mas_equalTo
,而相對於某個控件,或者某個控件的某個約束,我會使用equalTo
,如:make.size.mas_equalTo(CGSizeMake(100, 100));
make.center.equalTo(weakSelf.view);
setNeedsLayout
:告知頁面需要更新,但是不會立刻開始更新。執行後會立刻調用layoutSubviews。layoutIfNeeded
:告知頁面佈局立刻更新。所以一般都會和setNeedsLayout
一起使用。如果希望立刻生成新的frame需要調用此方法,利用這點一般佈局動畫可以在更新佈局後直接使用這個方法讓動畫生效。layoutSubviews
:系統重寫佈局setNeedsUpdateConstraints
:告知需要更新約束,但是不會立刻開始updateConstraintsIfNeeded
:告知立刻更新約束updateConstraints
:系統更新約束- (void)updateViewConstraints
ViewController的View在更新視圖佈局時,會先調用ViewController的updateViewConstraints 方法。我們可以通過重寫這個方法去更新當前View的內部佈局,而不用再繼承這個View去重寫-updateConstraints方法。我們在重寫這個方法時,務必要調用 super 或者 調用當前View的 -updateConstraints 方法。
1. 視圖居中顯示
// 防止block中的循環引用
__weak typeof(self) weakSelf = self;
UIView* view = [UIView new];
view.backgroundColor = [UIColor brownColor];
[self.view addSubview:view];
//使用mas_makeConstraints添加約束
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// 添加大小約束(make就是要添加約束的控件view)
make.size.mas_equalTo(CGSizeMake(200, 200));
// 添加居中約束(居中方式與self相同)
make.center.equalTo(weakSelf.view);
}];
2. 兩個視圖等寬高邊距
UIView* blackView = [UIView new];
blackView.backgroundColor = [UIColor blackColor];
[self.view addSubview:blackView];
[blackView mas_makeConstraints:^(MASConstraintMaker *make) {
//添加約束大小
make.size.mas_equalTo(CGSizeMake(100, 100));
//在 左,上 添加約束 (左、上約束都是20)
make.left.and.top.mas_equalTo(20);
}];
UIView* grayView = [UIView new];
grayView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:grayView];
[grayView mas_makeConstraints:^(MASConstraintMaker *make) {
// 大小、上邊距約束與黑色view相同
make.size.and.top.equalTo(blackView);
// 添加右邊距約束(這裏的間距是有方向性的,左、上邊距約束爲正數,右、下邊距約束爲負數)
make.right.mas_equalTo(-20);
}];
3. 鍵盤彈出和收回
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
__weak typeof(self) weakSelf = self;
_textField = [UITextField new];
_textField.backgroundColor = [UIColor redColor];
[self.view addSubview:_textField];
[_textField mas_makeConstraints:^(MASConstraintMaker *make) {
//left,right,centerx,y 不能共存只能有其二
make.left.mas_equalTo(20);
// make.right.mas_equalTo(-60);
make.centerX.equalTo(weakSelf.view);
make.height.mas_equalTo(40);
make.bottom.mas_equalTo(0);
}];
// 註冊鍵盤通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification {
// 獲取鍵盤基本信息(動畫時長與鍵盤高度)
NSDictionary *userInfo = [notification userInfo];
CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改下邊距約束
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-keyboardHeight);
}];
// 更新約束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardWillHideNotification:(NSNotification *)notification {
// 獲得鍵盤動畫時長
NSDictionary *userInfo = [notification userInfo];
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改爲以前的約束(距下邊距0)
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(0);
}];
// 更新約束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.view endEditing:YES];
}
4. 三控件等寬間距
方法一:
array 的 mas_distributeViewsAlongAxis withFixedSpacing
變化的是控件 長度或寬度
定義一個存放三個控件的數組NSArray *array;
array = @[greenView,redView,blueView];
直接調用下面的方法:
- (void)getHorizontalone
{
//方法一,array 的 mas_distributeViewsAlongAxis
/**
* 多個控件固定間隔的等間隔排列,變化的是控件的長度或者寬度值
*
* @param axisType 軸線方向
* @param fixedSpacing 間隔大小
* @param leadSpacing 頭部間隔
* @param tailSpacing 尾部間隔
*/
// MASAxisTypeHorizontal 水平
// MASAxisTypeVertical 垂直
[arrayList mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
withFixedSpacing:20
leadSpacing:5
tailSpacing:5];
[arrayList mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(60);
make.height.mas_equalTo(100);
}];
}
方法二:
array de mas_distributeViewsAlongAxis withFixedItemLength
控件size不變,變化的是間隙
- (void)getVertical
{
/**
* 多個固定大小的控件的等間隔排列,變化的是間隔的空隙
*
* @param axisType 軸線方向
* @param fixedItemLength 每個控件的固定長度或者寬度值
* @param leadSpacing 頭部間隔
* @param tailSpacing 尾部間隔
*/
[arrayList mas_distributeViewsAlongAxis:MASAxisTypeVertical
withFixedItemLength:60
leadSpacing:40
tailSpacing:10];
[arrayList mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.mas_equalTo(100);
// make.height.mas_equalTo(100);
make.left.mas_equalTo(20);
make.right.mas_equalTo(-20);
}];
}
以上倆方法都在NSArray+MASAdditions
中
方法三:直接設置multiplier
實現等間距
for (NSUInteger i = 0; i < 4; i++) {
UIView *itemView = [self getItemViewWithIndex:i];
[_containerView addSubview:itemView];
[itemView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.and.height.equalTo(@(ITEM_SIZE));
make.centerY.equalTo(_containerView.mas_centerY);
make.centerX.equalTo(_containerView.mas_right).multipliedBy(((CGFloat)i + 1) / ((CGFloat)ITEM_COUNT + 1));
}];
}
方法四: 利用透明等寬度的SpaceView實現等間距
UIView *lastSpaceView = [UIView new];
lastSpaceView.backgroundColor = [UIColor greenColor];
[_containerView1 addSubview:lastSpaceView];
[lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.top.and.bottom.equalTo(_containerView1);
}];
for (NSUInteger i = 0; i < ITEM_COUNT; i++) {
UIView *itemView = [self getItemViewWithIndex:i];
[_containerView1 addSubview:itemView];
[itemView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.and.width.equalTo(@(ITEM_SIZE));
make.left.equalTo(lastSpaceView.mas_right);
make.centerY.equalTo(_containerView1.mas_centerY);
}];
UIView *spaceView = [UIView new];
spaceView.backgroundColor = [UIColor greenColor];
[_containerView1 addSubview:spaceView];
[spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(itemView.mas_right).with.priorityHigh(); // 降低優先級,防止寬度不夠出現約束衝突
make.top.and.bottom.equalTo(_containerView1);
make.width.equalTo(lastSpaceView.mas_width);
}];
lastSpaceView = spaceView;
}
[lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_containerView1.mas_right);
}];
5. 動態改變字體寬度
和麪方法4一樣,利用spaceView來實現
UIView* bgView = [[UIView alloc]init];
bgView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:bgView];
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.mas_equalTo(0);
make.top.mas_equalTo(@100);
make.height.mas_equalTo(@100);
}];
listText = @[@"北京",@"地大吳波啊",@"你大爺",@"我們的愛哎哎"];
UIView *lastSpaceView = nil;
for(int i = 0 ; i < listText.count; i ++)
{
UILabel* label = [UILabel new];
label.text = listText[i];
label.backgroundColor = RANDOMCOLOR;
[bgView addSubview:label];
UIView* lineView = [UIView new];
lineView.backgroundColor = [UIColor redColor];
[bgView addSubview:lineView];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.mas_equalTo(0);
if (lastSpaceView)
{
NSLog(@"存在 lastView");
make.left.equalTo(lastSpaceView.mas_right).mas_offset(@20);
}else
{
NSLog(@"不存在存在 lastView");
make.left.equalTo(bgView.mas_left);
}
make.height.equalTo(bgView);
}];
lastSpaceView = label;
[lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.bottom.mas_equalTo(0);
make.width.mas_equalTo(1);
make.left.mas_equalTo(label.mas_right).mas_offset(@10);
}];
}
效果圖:
6. 父視圖的高度,是裏面倆控件高度的和
UIView* bgView = [UIView new];
bgView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:bgView];
UILabel* titleLab = [UILabel new];
titleLab.backgroundColor = [UIColor redColor];
titleLab.textAlignment = NSTextAlignmentCenter;
titleLab.font = [UIFont systemFontOfSize:15.f];
titleLab.text = @"曹操——《短歌行》";
[bgView addSubview:titleLab];
UILabel* contentLab = [UILabel new];
contentLab.numberOfLines = 0 ;
contentLab.textAlignment = NSTextAlignmentCenter;
contentLab.backgroundColor = [UIColor brownColor];
contentLab.font = [UIFont systemFontOfSize:13.f];
contentLab.text = @" 對酒當歌,人生幾何? 譬如朝露,去日苦多。\n 慨當以慷,憂思難忘。 何以解憂?唯有杜康。\n 青青子衿,悠悠我心。 但爲君故,沉吟至今。\n 呦呦鹿鳴,食野之苹。 我有嘉賓,鼓瑟吹笙。\n 明明如月,何時可掇? 憂從中來,不可斷絕。\n 越陌度阡,枉用相存。 契闊談宴,心念舊恩。\n 月明星稀,烏鵲南飛。 繞樹三匝,何枝可依?\n 山不厭高,海不厭深。 周公吐哺,天下歸心。";
[bgView addSubview:contentLab];
//思路: 父視圖的上間距等於title的上間距,父視圖的下間距等於content的下間距
__weak typeof(self) weakSelf = self;
[bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_offset(@30);
make.right.mas_offset(@-30);
make.centerY.equalTo(weakSelf.view);
}];
[titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.mas_equalTo(@0);
}];
[contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(@0);
make.top.equalTo(titleLab.mas_bottom).mas_offset(@10);
make.bottom.equalTo(bgView);
}];
效果圖:
以後慢慢更新,記錄方便以後使用
文/棟飛
//一些扒的別人的記錄
自適應佈局允許將寬度或高度設置爲固定值.如果你想要給視圖一個最小或最大值,你可以這樣:
//width >= 200 && width <= 400make.width.greaterThanOrEqualTo(@200);make.width.lessThanOrEqualTo(@400)
約束的優先級
.priority
允許你指定一個精確的優先級,數值越大優先級越高.最高1000..priorityHigh
等價於 UILayoutPriorityDefaultHigh
.優先級值爲 750..priorityMedium
介於高優先級和低優先級之間,優先級值在 250~750之間..priorityLow
等價於 UILayoutPriorityDefaultLow
, 優先級值爲 250.
優先級可以在約束的尾部添加:
make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);
center 中心
//使 centerX和 centerY = button1make.center.equalTo(button1)
//使 centerX = superview.centerX - 5, centerY = superview.centerY + 10make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))
指定寬度爲父視圖的 1/4.
make.width.equalTo(superview).multipliedBy(0.25);