必威-必威-欢迎您

必威,必威官网企业自成立以来,以策略先行,经营致胜,管理为本的商,业推广理念,一步一个脚印发展成为同类企业中经营范围最广,在行业内颇具影响力的企业。

3.必威1Masonry使用例子1,使用第三方先看看Mason

2019-09-16 00:01 来源:未知
@property (nonatomic, strong, readonly) MASConstraint *left; // 相邻左间距@property (nonatomic, strong, readonly) MASConstraint *top;@property (nonatomic, strong, readonly) MASConstraint *right;@property (nonatomic, strong, readonly) MASConstraint *bottom;@property (nonatomic, strong, readonly) MASConstraint *leading;// 头部 正常情况等价于left@property (nonatomic, strong, readonly) MASConstraint *trailing;// 尾部 正常情况等价于right@property (nonatomic, strong, readonly) MASConstraint *width;// 宽@property (nonatomic, strong, readonly) MASConstraint *height;@property (nonatomic, strong, readonly) MASConstraint *centerX; // 横向中点@property (nonatomic, strong, readonly) MASConstraint *centerY;@property (nonatomic, strong, readonly) MASConstraint *baseline; //文本基线#if TARGET_OS_IPHONE// 这写属性就相当于storyboard下面autolayout的pin设置的 对勾@property (nonatomic, strong, readonly) MASConstraint *leftMargin; // 在原来左间距的基础上所做出的间距@property (nonatomic, strong, readonly) MASConstraint *rightMargin;@property (nonatomic, strong, readonly) MASConstraint *topMargin;@property (nonatomic, strong, readonly) MASConstraint *bottomMargin;@property (nonatomic, strong, readonly) MASConstraint *leadingMargin;@property (nonatomic, strong, readonly) MASConstraint *trailingMargin;@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;#endif```好,下面用实例演示下:首先在Appdelegate中先写个window

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];self.window.rootViewController = [[ViewController alloc]init];```

3.1Masonry使用例子1

// exp1: 中心点与self.view相同,宽度为400*400
-(void)exp1{

    UIView *view = [UIView new];
    [view setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {

         make.center.equalTo(self.view);
         make.size.mas_equalTo(CGSizeMake(400,400));
    }];

}


//exp2: 上下左右边距都为10
-(void)exp2{

    UIView *view = [UIView new];
    [view setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

        //  make.left.equalTo(self.view).with.offset(10);
        //  make.right.equalTo(self.view).with.offset(-10);
        //  make.top.equalTo(self.view).with.offset(10);
        //  make.bottom.equalTo(self.view).with.offset(-10);
    }];

}

//exp3 让两个高度为150的view垂直居中且等宽且等间隔排列 间隔为10
-(void)exp3{

    UIView *view1 = [UIView new];
    [view1 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:view1];

    UIView *view2 = [UIView new];
    [view2 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:view2];

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(self.view.mas_centerY);
        make.height.mas_equalTo(150);
        make.width.mas_equalTo(view2.mas_width);
        make.left.mas_equalTo(self.view.mas_left).with.offset(10);
        make.right.mas_equalTo(view2.mas_left).offset(-10);

    }];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.mas_equalTo(self.view.mas_centerY);
        make.height.mas_equalTo(150);
        make.width.mas_equalTo(view1.mas_width);
        make.left.mas_equalTo(view1.mas_right).with.offset(10);
        make.right.equalTo(self.view.mas_right).offset(-10);

    }];

}

必威 1

exp1

必威 2

exp2

必威 3

exp3

The most important part

在.plist里导入<Masonry/Masonry.h>

YKAutolayout

  • Masonry 源码:

(一)Masonry介绍

Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X。 Masonry是一个用代码写iOS或os界面的库,可以代替Auto layout。 Masonry的github地址:https://github.com/SnapKit/Masonry

The grammar of Masonry is very easy, we don't need a lot of time to study it. By the way, Masony is still based on Autolayout API, but it offers more easily understand grammar for developers.
  1. Vist the largest Same-Gender social networking site--Github.
  2. Integrate Masonry to your project by Cocoapods.
  3. Import "Masonry.h"
  4. Use it follow its grammar.
  5. Start driving!

import <Masonry/Masonry.h>

链式语法

Masonry中一个很重要的特点是链式语法,最显著的特点是xx.xx.xx的连续调用,例如make.top.mas_equalTo(viewA.mas_bottom).offset;看到这里,可能会想,能不能把Masonry用链式语法写的更简洁一点呢?

TAG标签:
版权声明:本文由必威发布于必威-编程,转载请注明出处:3.必威1Masonry使用例子1,使用第三方先看看Mason