iOS之Masonry快速上手

快速上手

1.简介

Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,同时支持 iOS 和 Max OS X。Masonry是一个用代码写iOS或OS界面的库,可以代替Auto layout。
Masonry的github地址:Masonry!

2.配置

  • 推荐使用pods方式引入类库,pod ‘Masonry’,若不知道pod如何使用,自行谷歌
  • 引入头文件 #import “Masonry.h”

3.规则

mas_makeConstraints 是给view添加约束,约束有几种,分别是边距,宽,高,左上右下距离,基准线。添加过约束后可以有修正,修正有offset(位移)修正和multipliedBy(倍率)修正。
语法一般是 make.equalTo or make.greaterThanOrEqualTo or make.lessThanOrEqualTo + 倍数和位移修正。

4.注意点

  • 使用 mas_makeConstraints方法的元素必须事先添加到父元素的中,例如[self.view addSubview:view];
  • masequalTo 和 equalTo 区别:
    masequalTo 比 qualTo 多了类型转换操作,一般来说,大多数时候两个方法都是 通用的,但是对于数值元素使用mas_equalTo。对于对象或是多个属性的处理,使用equalTo。特别是多个属性时,必须使用equalTo,例如 make.left.and.right.equalTo(self.view);

5.实例

// 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.基本属性(Attribute)

@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; 左
@property (nonatomic, strong, readonly) MASConstraint
trailing; 右
@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 (IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)

@property (nonatomic, strong, readonly) MASConstraint firstBaseline;
@property (nonatomic, strong, readonly) MASConstraint
lastBaseline;

#endif

#if TARGET_OS_IPHONE || TARGET_OS_TV

@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;

可参考下图学习
Alt text

2.Constant

  • (MASConstraint * (^)(MASEdgeInsets insets))insets; 与四边边距

  • (MASConstraint * (^)(CGSize offset))sizeOffset;

  • (MASConstraint * (^)(CGPoint offset))centerOffset;

  • (MASConstraint * (^)(CGFloat offset))offset; —- 位移

  • (MASConstraint (^)(NSValue value))valueOffset; —- 以上值的组合

3.Multiplier(倍率)

(MASConstraint * (^)(CGFloat multiplier))multipliedBy; 与dividedBy互为倒数,方便计算

  • (MASConstraint * (^)(CGFloat divider))dividedBy;

4.Priority(优先级)

  • (MASConstraint * (^)(MASLayoutPriority priority))priority;

  • (MASConstraint * (^)())priorityLow;

  • (MASConstraint * (^)())priorityMedium;

(MASConstraint * (^)())priorityHigh;

5.intrinsicContentSize

(CGSize)intrinsicContentSize; — 默认尺寸大小
(void)invalidateIntrinsicContentSize — 使默认尺寸大小失效后重新计算

6.ContentPriority

  • (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
  • (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

  • (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

  • (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

可参考下图理解
Alt text

穷则github点星,达可兼济本人
显示 Gitment 评论
0%