iOS - Masonry使用中的一些整理

[置顶]iOS - Masonry使用中的一些整理

标签:iOS资源大全iOS常用方法iOS学习资料Masonry使用

2016-07-19 20:2211213人阅读评论(0)收藏举报

分类:

iOS的开发(484)

目录(?)[+]

个人喜欢用纯代码写东西,其中用到最多的就是砌体,我整理一些使用过程中一些点,方便以后使用。(基本的语法就不说了)

首先说几点:

我一般将数值类型的约束用mas_equalTo,而相对于某个控件,或者某个控件的某个约束,我会使用equalTo,如:

make.size.mas_equalTo(CGSizeMake(100, 100));

make.center.equalTo(weakSelf.view);

setNeedsLayout:告知页面需要更新,但是不会立刻开始更新。执行后会立刻调用layoutSubviews

layoutIfNeeded。:告知页面布局立刻更新。如此setNeedsLayout希望立刻生成新的框架需要调用此方法,利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效

layoutSubviews:系统重写布局

setNeedsUpdateConstraints:告知需要更新约束,但是不会立刻开始

updateConstraintsIfNeeded:告知立刻更新约束

updateConstraints:系统更新约束

- (void)updateViewConstraintsViewController的查看在更新视图布局时,会先调用ViewController的updateViewConstraints方法。我们可以通过重写这个方法去更新当前查看的内部布局,而不用再继承这个查看去重写-updateConstraints方法我们在重写这个方法时,务必要调用super或者调用当前View的-updateConstraints方法。

视图居中显示

// 防止block中的循环引用__weaktypeof(self) weakSelf =self;UIView* view        = [UIViewnew];view.backgroundColor= [UIColorbrownColor];[self.viewaddSubview:view];//使用mas_makeConstraints添加约束[view mas_makeConstraints:^(MASConstraintMaker *make) {// 添加大小约束(make就是要添加约束的控件view)make.size.mas_equalTo(CGSizeMake(200,200));// 添加居中约束(居中方式与self相同)make.center.equalTo(weakSelf.view);}];

两个视图等宽高边距

UIView* blackView      = [UIViewnew];blackView.backgroundColor= [UIColorblackColor];[self.viewaddSubview:blackView];[blackView mas_makeConstraints:^(MASConstraintMaker *make) {//添加约束大小make.size.mas_equalTo(CGSizeMake(100,100));//在 左,上 添加约束 (左、上约束都是20)make.left.and.top.mas_equalTo(20);}];UIView* grayView        = [UIViewnew];grayView.backgroundColor= [UIColorlightGrayColor];[self.viewaddSubview:grayView];[grayView mas_makeConstraints:^(MASConstraintMaker *make) {// 大小、上边距约束与黑色view相同make.size.and.top.equalTo(blackView);// 添加右边距约束(这里的间距是有方向性的,左、上边距约束为正数,右、下边距约束为负数)make.right.mas_equalTo(-20);}];

键盘弹出和收回

- (void)dealloc {[[NSNotificationCenterdefaultCenter] removeObserver:self];}- (void)viewDidLoad {[superviewDidLoad];// Do any additional setup after loading the view.__weaktypeof(self) weakSelf =self;_textField                = [UITextFieldnew];_textField.backgroundColor= [UIColorredColor];[self.viewaddSubview:_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);}];// 注册键盘通知[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotificationobject:nil];[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotificationobject:nil];}- (void)keyboardWillChangeFrameNotification:(NSNotification*)notification {// 获取键盘基本信息(动画时长与键盘高度)NSDictionary*userInfo = [notification userInfo];CGRectrect = [userInfo[UIKeyboardFrameBeginUserInfoKey]CGRectValue];CGFloatkeyboardHeight  =CGRectGetHeight(rect);CGFloatkeyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];// 修改下边距约束[_textField mas_updateConstraints:^(MASConstraintMaker *make) {make.bottom.mas_equalTo(-keyboardHeight);}];// 更新约束[UIViewanimateWithDuration:keyboardDuration animations:^{[self.viewlayoutIfNeeded];}];}- (void)keyboardWillHideNotification:(NSNotification*)notification {// 获得键盘动画时长NSDictionary*userInfo  = [notification userInfo];CGFloatkeyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];// 修改为以前的约束(距下边距0)[_textField mas_updateConstraints:^(MASConstraintMaker *make) {make.bottom.mas_equalTo(0);}];// 更新约束[UIViewanimateWithDuration:keyboardDuration animations:^{[self.viewlayoutIfNeeded];}];}- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {[supertouchesBegan:touches withEvent:event];[self.viewendEditing:YES];}

三控件等宽间距

方法一:

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:MASAxisTypeHorizontalwithFixedSpacing:20leadSpacing:5tailSpacing:5];[arrayList mas_makeConstraints:^(MASConstraintMaker *make) {make.top.mas_equalTo(60);make.height.mas_equalTo(100);}];

}

方法二:

array de mas_distributeViewsAlongAxis withFixedItemLength控件的大小不变,变化的是间隙

- (void)getVertical{/**

*  多个固定大小的控件的等间隔排列,变化的是间隔的空隙

*

*  @param axisType        轴线方向

*  @param fixedItemLength 每个控件的固定长度或者宽度值

*  @param leadSpacing    头部间隔

*  @param tailSpacing    尾部间隔

*/[arrayList mas_distributeViewsAlongAxis:MASAxisTypeVerticalwithFixedItemLength:60leadSpacing:40tailSpacing: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(NSUIntegeri =0; i <4; i++) {UIView*itemView = [selfgetItemViewWithIndex: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      = [UIViewnew];lastSpaceView.backgroundColor= [UIColorgreenColor];[_containerView1 addSubview:lastSpaceView];[lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.and.top.and.bottom.equalTo(_containerView1);}];for(NSUIntegeri =0; i < ITEM_COUNT; i++) {UIView*itemView = [selfgetItemViewWithIndex: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        = [UIViewnew];spaceView.backgroundColor= [UIColorgreenColor];[_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);}];

动态改变字体宽度

和面方法4一样,利用spaceView来实现

UIView* bgView      = [[UIViewalloc]init];bgView.backgroundColor= [UIColoryellowColor];[self.viewaddSubview: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(inti =0; i < listText.count;  i ++){UILabel* label = [UILabelnew];label.text= listText[i];label.backgroundColor= RANDOMCOLOR;[bgView addSubview:label];


UIView* lineView        = [UIViewnew];lineView.backgroundColor= [UIColorredColor];[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);}];}



效果图:

父视图的高度,是里面俩控制高度的和

UIView* bgView      = [UIViewnew];bgView.backgroundColor= [UIColorpurpleColor];[self.viewaddSubview:bgView];UILabel* titleLab        = [UILabelnew];titleLab.backgroundColor= [UIColorredColor];titleLab.textAlignment=NSTextAlignmentCenter;titleLab.font= [UIFontsystemFontOfSize:15.f];titleLab.text=@"曹操——《短歌行》";[bgView addSubview:titleLab];UILabel* contentLab        = [UILabelnew];contentLab.numberOfLines=0;contentLab.textAlignment=NSTextAlignmentCenter;contentLab.backgroundColor= [UIColorbrownColor];contentLab.font= [UIFontsystemFontOfSize:13.f];contentLab.text=@" 对酒当歌,人生几何? 譬如朝露,去日苦多。\n 慨当以慷,忧思难忘。 何以解忧?唯有杜康。\n 青青子衿,悠悠我心。 但为君故,沉吟至今。\n 呦呦鹿鸣,食野之苹。 我有嘉宾,鼓瑟吹笙。\n 明明如月,何时可掇? 忧从中来,不可断绝。\n 越陌度阡,枉用相存。 契阔谈宴,心念旧恩。\n 月明星稀,乌鹊南飞。 绕树三匝,何枝可依?\n 山不厌高,海不厌深。 周公吐哺,天下归心。";[bgView addSubview:contentLab];//思路: 父视图的上间距等于title的上间距,父视图的下间距等于content的下间距__weaktypeof(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);

中心中心

//使centerX和centerY = button1

make.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);

文/魏同学(简书作者)

原文链接:HTTP://www.jianshu.com/p/a24dd8638d28

著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容