iOS:Masonry练习详解

摘要:
有几种不同的方法可以更新砌体中的约束。

Masonry练习详解

 
添加约束的方式:
1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConstraints = NO,即取消自动布局;
 
2.通过使用MASConstraintMaker在block中添加约束,不需要再设置translatesAutoresizingMaskIntoConstraintst 属性,block内部已经帮助完成;
 
 
约束的关系:

equalTo  <=======>   NSLayoutRelationEqual   等于

lessThanOrEqualTo   <======>  NSLayoutRelationLessThanOrEqual   小于或等于

greaterThanOrEqualTo <=======>  NSLayoutRelationGreaterThanOrEqual  大于或等于

 

MASViewAttribute:视图约束属性

iOS:Masonry练习详解第1张
 
 
UIView/NSView
这两个约束完全相同,都是view左边大于等于label的左边位置
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);
 
 
NSNumber给约束设置具体的值
<1>//width >= 200 && width <= 400
make.width.greaterThanOrEqualTo(@200);
make.width.lessThanOrEqualTo(@400)
<2>//creates view.left = view.superview.left + 10
make.left.lessThanOrEqualTo(@10)
代替NSNumber,使用原始的数据或者结构体设置约束数据
make.top.mas_equalTo(42);
make.height.mas_equalTo(20);
make.size.mas_equalTo(CGSizeMake(50, 100));
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));
 
使用数组NSArray设置约束
make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);
 
 
使用优先级设置约束

.priorityHigh <======> UILayoutPriorityDefaultHigh     高优先级

.priorityMedium <========> between high and low        介于高/低之间

.priorityLow <=========> UILayoutPriorityDefaultLow   低优先级

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);
 
使用MASCompositeConstraints添加约束
edges:边缘
// make top, left, bottom, right equal view2
    make.edges.equalTo(view2);

// make top = superview.top + 5, left = superview.left + 10,
// bottom = superview.bottom - 15, right = superview.right - 20
    make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))
// All edges but the top should equal those of the superview
make.left.right.and.bottom.equalTo(superview);
make.top.equalTo(otherView);
 
size:大小
// make width and height greater than or equal to titleLabel
    make.size.greaterThanOrEqualTo(titleLabel)

// make width = superview.width + 100, height = superview.height - 50
    make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))
 
center:中心
// make centerX and centerY = button1
    make.center.equalTo(button1)

// make centerX = superview.centerX - 5, centerY = superview.centerY + 10
    make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))
 
 
有时候,你需要修改现有的约束,以动画或删除/替换约束。在砌体中有几个不同的方法来更新约束。
1.使用设置References
// in public/private interface
@property (nonatomic, strong) MASConstraint *topConstraint;
...
// when making constraints
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
}];
...
// then later you can call
[self.topConstraint uninstall];
 
2.更新约束 mas_updateConstraints
- (void)updateConstraints {
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(self.buttonSize.width)).priorityLow();
        make.height.equalTo(@(self.buttonSize.height)).priorityLow();
        make.width.lessThanOrEqualTo(self);
        make.height.lessThanOrEqualTo(self);
    }];

    //according to apple super should be called at end of method
    [super updateConstraints];
}
 
3.重新设置mas_remakeConstraints
- (void)changeButtonPosition {
    [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.buttonSize);

        if (topLeft) {
            make.top.and.left.offset(10);
        } else {
            make.bottom.and.right.offset(-10);
        }
    }];
}

 具体的实例如下:设置view1举例父视图的四周距离均为50

 方式一:
/**
方式一:使用NSLayoutConstraint实现手动布局
 
 ----------------------------
 设置UIEdgeInsets
 ----------------------------
 
 @interface UIView (UIConstraintBasedLayoutInstallingConstraints)
 - (NSArray *)constraints NS_AVAILABLE_IOS(6_0);
 - (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0);
 - (void)addConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0);
 - (void)removeConstraint:(NSLayoutConstraint *)constraint
 - (void)removeConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0);
*/
-(void)LayoutConstraint
{
    UIView *superView = self.view;
    UIView *view1 = [[UIView alloc]init];
    view1 .translatesAutoresizingMaskIntoConstraints = NO;
    view1.backgroundColor = [UIColor redColor];
    [superView addSubview:view1];
    
    //设置距离父视图边界距离
    UIEdgeInsets pading = UIEdgeInsetsMake(50, 50, 50, 50);
    
    //添加给view1约束
    [superView addConstraints:@[
                                
        [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:pading.top],
        
        [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:pading.left],
        
        [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-pading.bottom],
        
        [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-pading.right],
    ]];
}
 方式二:
/**
方法二:使用block  @implementation MAS_VIEW (MASAdditions)
 
----------------------------
 设置offset偏移 或者 边缘edges
----------------------------
 
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block 
*/
-(void)LayoutForMASConstraintMaker
{
    UIView *superView = self.view;
    UIView *view1 = [[UIView alloc]init];
    view1.backgroundColor = [UIColor redColor];
    [superView addSubview:view1];
    
    //设置距离父视图边界距离
    UIEdgeInsets pading = UIEdgeInsetsMake(50, 50, 50, 50);
    
    //添加给view1约束
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(superView.mas_top).with.offset(pading.top);
        make.left.equalTo(superView.mas_left).with.offset(pading.left);
        make.bottom.equalTo(superView.mas_bottom).with.offset(-pading.bottom);
        make.right.equalTo(superView.mas_right).with.offset(-pading.right);
        
        //设置代码可以更简单(效果与上面的是一样的)
        //make.edges.equalTo(superView).with.insets(pading);
    }];
}
 方式三:
/**
方式三:使用block  @implementation MAS_VIEW (MASAdditions)
 
 ----------------------------
 设置margin距离
 ----------------------------
 
 
 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block
 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block
 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block
*/
-(void)LayoutForMASConstraintMakerWithMargin
{
    UIView *superView = self.view;
    UIView *view1 = [[UIView alloc]init];
    view1.backgroundColor = [UIColor redColor];
    [superView addSubview:view1];
    
    //添加给view1约束
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.topMargin.equalTo(superView.mas_top).with.offset(50);
        make.leftMargin.equalTo(superView.mas_left).with.offset(50);
        make.bottomMargin.equalTo(superView.mas_bottom).with.offset(-50);
        make.rightMargin.equalTo(superView.mas_right).with.offset(-50);
    }];
}
 方式四:
/**
 方式四:使用block  @implementation MAS_VIEW (MASAdditions)
 
 
 ----------------------------
 设置center和size
 ----------------------------

 
 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block
 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block
 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block
 */
-(void)LayoutForMASConstraintMakerWithCenterWidthHeight
{
    UIView *superView = self.view;
    UIView *view1 = [[UIView alloc]init];
    view1.backgroundColor = [UIColor redColor];
    [superView addSubview:view1];
    
    //添加给view1约束
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(superView);
        make.centerY.equalTo(superView);
        make.size.equalTo(superView).sizeOffset(CGSizeMake(-100,-100));
    }];
}
 演示结果:
 iOS:Masonry练习详解第2张 iOS:Masonry练习详解第3张
 
 
 
 
 
 
 
 

免责声明:文章转载自《iOS:Masonry练习详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇将Latex tex文档转换成 word文档(上)Gradle 配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

OC常用数据类型大全解

UI基础OC常用数据类型 Block Block封装了一段代码,可以在任何时候执行 Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值。它和传统的函数指针很类似,但是有区别:block是inline(内联函数)的,并且默认情况下它对局部变量是只读的 苹果官方建议尽量多用block。在多线程、异步任务、集合遍历、集合排序、动画转场用...

iOS开发之Block

1、定义 (1) Block是OC中的一种数据类型,在iOS开发中被广泛使用 (2) ^是Block的特有标记 (3) Block的实现代码包含在{}之间 (4) 大多情况下,以内联inline函数的方式被定义和使用 (5) Block与C语言的函数指针有些相似,但使用起来更加灵活 例如: void(^demoBlock)() = ^ {     NSLo...

iOS中Block的用法,举例,解析与底层原理

1. 前言 Block:带有自动变量(局部变量)的匿名函数。它是C语言的扩充功能。之所以是拓展,是因为C语言不允许存在这样匿名函数。 1.1 匿名函数 匿名函数是指不带函数名称函数。C语言中,函数是怎样的呢?类似这样: int func(int count); 调用的时候: int result = func(10); func就是它的函数名。也可以通...

IOS学习3——代理

本文转载自:你真的了解iOS代理设计模式吗? 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递一些参数。这篇文章会涵盖代理的使用技巧和原理,以及代理的内存管理等方面的知识。我会通过这些方面的知识,带大家真正领略代理的奥妙。写的有点多,但都是干货,我能写下去,不知道你有没有耐心看下去。本人能力有限,如果文章中有什...

IOS学习4——block代码块

本文转载自:iOS开发-由浅至深学习block 一、关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调。这不免让我们想到在C函数中,我们可以定义一个指向函数的指针并且调用: 1 bool executeSomeTask(void) { 2 //do somethin...

超全!iOS 面试题汇总

作者:Job_Yang 之前看了很多面试题,感觉要不是不够就是过于冗余,于是我将网上的一些面试题进行了删减和重排,现在分享给大家。(题目来源于网络,侵删) 1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: Object-c的类不可以多重继承;可以实现多个接口,通过实现多...