IOS约束

使用代码实现Autolayout的方法

  • 创建约束
+(id)constraintWithItem:(id)view1attribute:(NSLayoutAttribute)attr1relatedBy:(NSLayoutRelation)relationtoItem:(id)view2attribute:(NSLayoutAttribute)attr2multiplier:(CGFloat)multiplierconstant:(CGFloat)c;* view1 :要约束的控件* attr1 :约束的类型(做怎样的约束)* relation :与参照控件之间的关系* view2 :参照的控件* attr2 :约束的类型(做怎样的约束)* multiplier :乘数* c :常量
  • 添加约束
 - (void)addConstraint:(NSLayoutConstraint *)constraint;- (void)addConstraints:(NSArray *)constraints;
  • 注意 一定要在拥有父控件之后再添加约束
    关闭Autoresizing功能(非常有用,很多时候布局出来得不到想要的结果都是因为系统自动布局功能没有关闭)view.translatesAutoresizingMaskIntoConstraints = NO;


使用代码实现Autolayout的方法2 - VFL
使用VFL创建约束数组
+ (NSArray *)constraintsWithVisualFormat:(NSString *)formatoptions:(NSLayoutFormatOptions)optsmetrics:(NSDictionary *)metricsviews:(NSDictionary *)views;

  • format :VFL语句*
  • opts :约束类型* metrics :VFL语句中用到的具体数值*
  • views :VFL语句中用到的控件
    使用下面的宏来自动生成views和metrics参数
    NSDictionaryOfVariableBindings(...)

VFL语句 研究代码

.h
//
//  ViewController.h
//  Constraint
//
//  Created by 陶亚利 on 16/6/29.
//  Copyright © 2016年 陶亚利. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

.m
//
//  ViewController.m
//  Constraint
//
//  Created by 陶亚利 on 16/6/29.
//  Copyright © 2016年 陶亚利. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    
    UIView *firstView;
    UIView *secondView;
    UIView *thirdView;
    
    
    UIView *view1;
    UIView *view2;
    UIView *view3;
    UIView *view4;
    UIView *view5;
}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    /**
//     第一个view
//     */
//    firstView = [[UIView alloc]init];
//    firstView.translatesAutoresizingMaskIntoConstraints = NO;
//    firstView.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:firstView];
//    
//    /**
//     第二个view
//     */
//    secondView = [[UIView alloc]init];
//    secondView.translatesAutoresizingMaskIntoConstraints = NO;
//    secondView.backgroundColor = [UIColor brownColor];
//    [self.view addSubview:secondView];
//    
//    /**
//     第三个view
//     */
//    thirdView = [[UIView alloc]init];
//    thirdView.translatesAutoresizingMaskIntoConstraints = NO;
//    thirdView.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:thirdView];
//    
//    /**绑定三个view*/
//    NSDictionary *dic_bind = NSDictionaryOfVariableBindings(firstView,secondView,thirdView);
//    /**设置view之间的间距和高度*/
//    NSDictionary *dic_Constraint = @{ @"padding":@(10.f),
//                                      @"height":@(150.f)
//                                      };
//    
//    /**
//     *  第一个view添加约束
//     */
//    /**垂直方向居中对齐*/
//    NSLayoutConstraint *first_CenterY = [NSLayoutConstraint constraintWithItem:firstView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
//    
//    /**垂直方向添加高度约束*/
//    NSArray *first_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[firstView(height)]" options:0 metrics:dic_Constraint views:dic_bind];
//    [self.view addConstraints:@[first_CenterY]];
//    [self.view addConstraints:first_V];
//    
//    
//    /**
//     *  第二个view添加约束
//     */
//    /**垂直方向居中对齐*/
//    NSLayoutConstraint *second_CenterY = [NSLayoutConstraint constraintWithItem:secondView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
//    
//    /**垂直方向添加高度约束*/
//    NSArray *second_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[secondView(height)]" options:0 metrics:dic_Constraint views:dic_bind];
//    [self.view addConstraint:second_CenterY];
//    [self.view addConstraints:second_V];
//    
//    /**
//     *  第三个view添加约束
//     */
//    /**垂直方向居中对齐*/
//    NSLayoutConstraint *third_CenterY = [NSLayoutConstraint constraintWithItem:thirdView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
//    
//    /**垂直方向添加高度约束*/
//    NSArray *third_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[thirdView(height)]" options:0 metrics:dic_Constraint views:dic_bind];
//    [self.view addConstraints:@[third_CenterY]];
//    [self.view addConstraints:third_V];
//    
//    /**给三个view添加水平约束等宽等间距*/
//    NSArray * allConstraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padding-[firstView]-10-[secondView(firstView)]-padding-[thirdView(secondView)]-padding-|" options:0 metrics:dic_Constraint views:dic_bind];
//    [self.view addConstraints:allConstraint_H];
    
    
    
//    return;
    
    
    
    view1 = [[UIView alloc] init];
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    view1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view1];
    
    
    view2 = [[UIView alloc] init];
    view2.translatesAutoresizingMaskIntoConstraints = NO;
    view2.backgroundColor = [UIColor redColor];
    [self.view addSubview:view2];
    
    view3 = [[UIView alloc] init];
    view3.translatesAutoresizingMaskIntoConstraints = NO;
    view3.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view3];
    
    
    view4 = [[UIView alloc] init];
    view4.translatesAutoresizingMaskIntoConstraints = NO;
    view4.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view4];
    
    view5 = [[UIView alloc] init];
    view5.translatesAutoresizingMaskIntoConstraints = NO;
    view5.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view5];
    
    
    
//    [self addTextConstraints];
    
    [self addTextConstraints2];
}

- (void) addTextConstraints{
    
    
    //     "H:|[page1(==view)][page2(==view)][page3(==view)][page4(==view)][page5(==view)]|", options: .AlignAllTop | .AlignAllBottom, metrics: nil, views: views)
    
    
    NSDictionary *widthAndHeight = @{@"spaceH":@(20.f),@"height":@(100.f)};
    
    //    NSArray *arrH1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(spaceH)-[view1(>=50)]-(spaceH)-[view2]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2)];
    
    NSArray *arrV1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view1(height)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1)];
    
    //    [self.view addConstraints:arrH1];
    [self.view addConstraints:arrV1];
    
    //    NSArray *arrH2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20)-[view1(>=50)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(view1)];
    //    NSArray *arrV2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(20)-[view1(100)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(view1)];
    
    
    
    
    //    NSLayoutConstraint *layoutL = [NSLayoutConstraint constraintWithItem:blueView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:(NSLayoutAttributeLeft) multiplier:1 constant:10];
    
    
    //    NSLayoutConstraint *view2left = [NSLayoutConstraint constraintWithItem:view2 attribute:(NSLayoutAttributeLeft) relatedBy:NSLayoutRelationLessThanOrEqual toItem:view1 attribute:(NSLayoutAttributeRight) multiplier:1 constant:20];
    //
    //    NSLayoutConstraint *view2right = [NSLayoutConstraint constraintWithItem:view2 attribute:(NSLayoutAttributeRight) relatedBy:NSLayoutRelationEqual toItem:self.view attribute:(NSLayoutAttributeRight) multiplier:1 constant:-20];
    //
    //    NSArray *arrH2 = @[
    //                       view2left,
    //                       view2right
    //                       ];
    
    //    NSArray *arrH2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view1]-(spaceH)-[view2(>=50)]-(spaceH)-[view3]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2,view3)];
    
    NSArray *arrV2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view2(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view2)];
    
    
    //    [self.view addConstraints:arrH2];
    [self.view addConstraints:arrV2];
    
    
    
    //    NSArray *arrH3 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view2]-(spaceH)-[view3(>=50)]-(spaceH)-[view4]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view2,view3,view4)];
    
    NSArray *arrV3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view3(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view3)];
    
    
    //    [self.view addConstraints:arrH3];
    [self.view addConstraints:arrV3];
    
    
    
    
    
    //    NSArray *arrH4 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view3]-(spaceH)-[view4(>=50)]-(spaceH)-[view5]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view3,view4,view5)];
    
    NSArray *arrV4 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view4(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view4)];
    
    
    //    [self.view addConstraints:arrH4];
    [self.view addConstraints:arrV4];
    
    
    
    
    
    //    NSArray *arrH5 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view4]-(spaceH)-[view5(>=50)]-(spaceH)-|" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view4,view5)];
    
    NSArray *arrV5 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view5(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view5)];
    //    [self.view addConstraints:arrH5];
    [self.view addConstraints:arrV5];
    
    NSArray *arrWedth = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(spaceH)-[view1]-(spaceH)-[view2(view1)]-(spaceH)-[view3(view1)]-(spaceH)-[view4(view1)]-(spaceH)-[view5(view1)]-(spaceH)-|" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2,view3,view4,view5)];
    
    [self.view addConstraints:arrWedth];

}



- (void) addTextConstraints2{
    
    NSDictionary *widthAndHeight = @{@"spaceH":@(20.f),@"height":@(100.f),@"Fheight":@(-100.f)};
    
    //    NSArray *arrH1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(spaceH)-[view1(>=50)]-(spaceH)-[view2]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2)];
    
    NSArray *arrV1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view1(height)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1)];
    
    //    [self.view addConstraints:arrH1];
    [self.view addConstraints:arrV1];
    
    
//    //    NSArray *arrH2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view1]-(spaceH)-[view2(>=50)]-(spaceH)-[view3]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2,view3)];
//    
//    NSArray *arrV2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view2(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view2)];
//    
//    
//    //    [self.view addConstraints:arrH2];
//    [self.view addConstraints:arrV2];
//    
//    
//    
//    //    NSArray *arrH3 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view2]-(spaceH)-[view3(>=50)]-(spaceH)-[view4]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view2,view3,view4)];
//    
//    NSArray *arrV3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view3(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view3)];
//    
//    
//    //    [self.view addConstraints:arrH3];
//    [self.view addConstraints:arrV3];
//    
//    
//    
//    
//    
//    //    NSArray *arrH4 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view3]-(spaceH)-[view4(>=50)]-(spaceH)-[view5]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view3,view4,view5)];
//    
//    NSArray *arrV4 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view4(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view4)];
//    
//    
//    //    [self.view addConstraints:arrH4];
//    [self.view addConstraints:arrV4];
//    
//    
//    
//    
//    
//    //    NSArray *arrH5 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view4]-(spaceH)-[view5(>=50)]-(spaceH)-|" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view4,view5)];
//    
//    NSArray *arrV5 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spaceH)-[view5(100)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view5)];
//    //    [self.view addConstraints:arrH5];
//    [self.view addConstraints:arrV5];
    
    
    NSArray *arrWedth = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(spaceH)-[view1]-(spaceH)-[view2(view1)]-(spaceH)-[view3(view1)]-(spaceH)-[view4(view1)]-(spaceH)-[view5(view1)]-(spaceH)-|" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2,view3,view4,view5)];
    
    [self.view addConstraints:arrWedth];
    
    
//    NSArray *arrV =     [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view1]-(height)-[view2(view1)]-(height)-[view3(view1)]-(height)-[view4(view1)]-(height)-[view5(view1)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2,view3,view4,view5)];

    NSArray *arrV =     [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view1]-(Fheight)-[view2(view1)]-(Fheight)-[view3(view1)]-(Fheight)-[view4(view1)]-(Fheight)-[view5(view1)]" options:0 metrics:widthAndHeight views:NSDictionaryOfVariableBindings(view1,view2,view3,view4,view5)];
    
    [self.view addConstraints:arrV];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end





使用代码实现Autolayout的方法3 - Masonry(重点,这套框架还是响应式函数编程的典范,研究响应式函数编程必学)

  • 使用步骤
    1.添加Masonry文件夹的所有源代码到项目中
    2.添加2个宏、导入主头文件
    // 只要添加了这个宏,就不用带mas_前缀

define MAS_SHORTHAND

// 只要添加了这个宏,equalTo就等价于mas_equalTo

define MAS_SHORTHAND_GLOBALS

// 这个头文件一定要放在上面两个宏的后面

import "Masonry.h"

  • 添加约束的方法
    // 这个方法只会添加新的约束
    [view makeConstraints:^(MASConstraintMaker *make) { }];
    // 这个方法会将以前的所有约束删掉,添加新的约束
    [view remakeConstraints:^(MASConstraintMaker *make) { }];
    // 这个方法将会覆盖以前的某些特定的约束
    [view updateConstraints:^(MASConstraintMaker *make) { }];
  • 约束的类型
    1.尺寸:width\height\size
    2.边界:left\leading\right\trailing\top\bottom
    3.中心点:center\centerX\centerY
    4.边界:edges
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容