個(gè)人喜歡用純代碼寫(xiě)東西,其中用到最多的就是Masonry,我整理一些使用過(guò)程中一些點(diǎn),方便以后使用.(基本的語(yǔ)法就不說(shuō)了)
首先說(shuō)幾點(diǎn):
- 我一般將數(shù)值類(lèi)型的約束用
mas_equalTo
,而相對(duì)于某個(gè)控件碴里,或者某個(gè)控件的某個(gè)約束搔驼,我會(huì)使用equalTo
嗤军,如:
make.size.mas_equalTo(CGSizeMake(100, 100));
make.center.equalTo(weakSelf.view);
-
setNeedsLayout
:告知頁(yè)面需要更新靶瘸,但是不會(huì)立刻開(kāi)始更新寝优。執(zhí)行后會(huì)立刻調(diào)用layoutSubviews条舔。
layoutIfNeeded
:告知頁(yè)面布局立刻更新。所以一般都會(huì)和setNeedsLayout
一起使用乏矾。如果希望立刻生成新的frame需要調(diào)用此方法孟抗,利用這點(diǎn)一般布局動(dòng)畫(huà)可以在更新布局后直接使用這個(gè)方法讓動(dòng)畫(huà)生效。
layoutSubviews
:系統(tǒng)重寫(xiě)布局
setNeedsUpdateConstraints
:告知需要更新約束钻心,但是不會(huì)立刻開(kāi)始
updateConstraintsIfNeeded
:告知立刻更新約束
updateConstraints
:系統(tǒng)更新約束 -
- (void)updateViewConstraints
ViewController的View在更新視圖布局時(shí)凄硼,會(huì)先調(diào)用ViewController的updateViewConstraints 方法。我們可以通過(guò)重寫(xiě)這個(gè)方法去更新當(dāng)前View的內(nèi)部布局捷沸,而不用再繼承這個(gè)View去重寫(xiě)-updateConstraints方法摊沉。我們?cè)谥貙?xiě)這個(gè)方法時(shí),務(wù)必要調(diào)用 super 或者 調(diào)用當(dāng)前View的 -updateConstraints 方法痒给。
1. 視圖居中顯示
// 防止block中的循環(huán)引用
__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. 兩個(gè)視圖等寬高邊距
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);
// 添加右邊距約束(這里的間距是有方向性的苍柏,左尼斧、上邊距約束為正數(shù),右试吁、下邊距約束為負(fù)數(shù))
make.right.mas_equalTo(-20);
}];
3. 鍵盤(pán)彈出和收回
- (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);
}];
// 注冊(cè)鍵盤(pán)通知
[[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 {
// 獲取鍵盤(pán)基本信息(動(dòng)畫(huà)時(shí)長(zhǎng)與鍵盤(pán)高度)
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 {
// 獲得鍵盤(pán)動(dòng)畫(huà)時(shí)長(zhǎng)
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
變化的是控件 長(zhǎng)度或?qū)挾?/p>
定義一個(gè)存放三個(gè)控件的數(shù)組NSArray *array;
array = @[greenView,redView,blueView];
注意:
數(shù)組里面的元素不能小于2個(gè),要不會(huì)報(bào)錯(cuò) views to distribute need to bigger than one
直接調(diào)用下面的方法:
- (void)getHorizontalone
{
//方法一,array 的 mas_distributeViewsAlongAxis
/**
* 多個(gè)控件固定間隔的等間隔排列棺棵,變化的是控件的長(zhǎng)度或者寬度值
*
* @param axisType 軸線(xiàn)方向
* @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
{
/**
* 多個(gè)固定大小的控件的等間隔排列,變化的是間隔的空隙
*
* @param axisType 軸線(xiàn)方向
* @param fixedItemLength 每個(gè)控件的固定長(zhǎng)度或者寬度值
* @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
中
方法三:直接設(shè)置multiplier
實(shí)現(xiàn)等間距
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實(shí)現(xiàn)等間距
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(); // 降低優(yōu)先級(jí),防止寬度不夠出現(xiàn)約束沖突
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. 動(dòng)態(tài)改變字體寬度
和面方法4一樣,利用spaceView來(lái)實(shí)現(xiàn)
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 = @[@"北京",@"地大吳波啊",@"你大爺",@"我們的愛(ài)哎哎"];
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. 父視圖的高度,是里面?zhèn)z控件高度的和
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 = @" 對(duì)酒當(dāng)歌熄捍,人生幾何烛恤? 譬如朝露,去日苦多余耽。\n 慨當(dāng)以慷缚柏,憂(yōu)思難忘。 何以解憂(yōu)宾添?唯有杜康船惨。\n 青青子衿柜裸,悠悠我心缕陕。 但為君故粱锐,沉吟至今。\n 呦呦鹿鳴扛邑,食野之蘋(píng)怜浅。 我有嘉賓,鼓瑟吹笙蔬崩。\n 明明如月恶座,何時(shí)可掇? 憂(yōu)從中來(lái)沥阳,不可斷絕跨琳。\n 越陌度阡,枉用相存桐罕。 契闊談宴脉让,心念舊恩。\n 月明星稀功炮,烏鵲南飛溅潜。 繞樹(shù)三匝,何枝可依薪伏?\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);
}];
效果圖:
以后慢慢更新,記錄方便以后使用
<br />
文/棟飛
//一些扒的別人的記錄
自適應(yīng)布局允許將寬度或高度設(shè)置為固定值.如果你想要給視圖一個(gè)最小或最大值,你可以這樣:
//width >= 200 && width <= 400 make.width.greaterThanOrEqualTo(@200); make.width.lessThanOrEqualTo(@400)
約束的優(yōu)先級(jí)
.priority
允許你指定一個(gè)精確的優(yōu)先級(jí),數(shù)值越大優(yōu)先級(jí)越高.最高1000.
.priorityHigh
等價(jià)于 UILayoutPriorityDefaultHigh
.優(yōu)先級(jí)值為 750.
.priorityMedium
介于高優(yōu)先級(jí)和低優(yōu)先級(jí)之間,優(yōu)先級(jí)值在 250~750之間.
.priorityLow
等價(jià)于 UILayoutPriorityDefaultLow
, 優(yōu)先級(jí)值為 250.
優(yōu)先級(jí)可以在約束的尾部添加:
make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);
center 中心
//使 centerX和 centerY = button1
make.center.equalTo(button1)
//使 centerX = superview.centerX - 5, centerY = superview.centerY + 10 make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))
指定寬度為父視圖的 1/4.
make.width.equalTo(superview).multipliedBy(0.25);