美文网首页
iOS - Masonry使用中的一些整理

iOS - Masonry使用中的一些整理

作者: DreamMakerSky | 来源:发表于2017-07-04 09:20 被阅读0次

    [置顶]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

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

    相关文章

      网友评论

          本文标题: iOS - Masonry使用中的一些整理

          本文链接:https://www.haomeiwen.com/subject/mnethxtx.html