NSLayoutConstraint 动态布局

来源:互联网 发布:iphone6网络很慢 编辑:程序博客网 时间:2024/06/04 18:12

    在iOS开发中,由于屏幕是变化的,所以有时候 布局是动态的,所以本期带来的就是NSLayoutConstraint动态布局。

    在用NSLayoutConstraint布局之前translatesAutoresizingMaskIntoConstraints属性一定要设为NO。

  先介绍一下用的一个方法。

+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullableid)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

参数view1:目标视图。

参数attr1:目标视图所要设置的属性。

参数relation:目标视图属性与参照视图的关系 有三种

    NSLayoutRelationLessThanOrEqual = -1,小于等于

    NSLayoutRelationEqual = 0,等于

    NSLayoutRelationGreaterThanOrEqual = 1,大于等于

参数view2:参照视图。

参数attr2:参照视图属性。

参数multiplier:乘数--目标视图与参照视图的属性的倍数关系。不可为0

参数c:常数--目标视图与参照视图的属性的加减关系。

属性NSLayoutAttribute:

    NSLayoutAttributeLeft      对象左边距

    NSLayoutAttributeRight,     对象右边距

    NSLayoutAttributeTop,对象上边距

    NSLayoutAttributeBottom,    对象下边距

    NSLayoutAttributeLeading,对象左边距

    NSLayoutAttributeTrailing,对象右边距 这里特别将强调一下trailing和right代表的意思是一样的但是实际应用的时候 不能混用 一定要统一

    NSLayoutAttributeWidth,对象的宽

    NSLayoutAttributeHeight,对象的高

    NSLayoutAttributeCenterX,对象中心点的x

    NSLayoutAttributeCenterY,   对象中心的点y

    NSLayoutAttributeLastBaseline,文本下划线

    NSLayoutAttributeBaseline NS_SWIFT_UNAVAILABLE("Use 'lastBaseline' instead") = NSLayoutAttributeLastBaseline,文本下划线

    NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),文本上划线

    对于下面的属性在上面的基础上加了边缘,也就是说上下左右距离实际边缘有8磅的距离。

    NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeNotAnAttribute = 0没有属性


    好了说了那么多废话,终于要应用了,这样吧随便举个例子:在屏幕中央并列横排放三个label,所有的宽与所有的高都相同,居左8,居右8,间距10。

    下面是实现代码:

    UILabel *label1 = [[UILabelalloc] init];

    UILabel *label2 = [[UILabelalloc] init];

    UILabel *label3 = [[UILabelalloc] init];

    [self.viewaddSubview:label1];

    [self.viewaddSubview:label2];

    [self.viewaddSubview:label3];

    label1.translatesAutoresizingMaskIntoConstraints =NO;

    label2.translatesAutoresizingMaskIntoConstraints =NO;

    label3.translatesAutoresizingMaskIntoConstraints =NO;

    label1.backgroundColor = [UIColorgreenColor];

    label2.backgroundColor = [UIColoryellowColor];

    label3.backgroundColor = [UIColorredColor];

    label1.text =@"第一个";

    label2.text =@"第二个";

    label3.text =@"第三个";

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label1attribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:label1 attribute:NSLayoutAttributeWidthmultiplier:1constant:0]];//label1高=宽

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label1attribute:NSLayoutAttributeLeftMarginrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeLeftMarginmultiplier:1constant:0]];// label1距左8

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label1attribute:NSLayoutAttributeCenterYrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeCenterYmultiplier:1constant:0]];//label1距上150

    

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label2attribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:label2 attribute:NSLayoutAttributeWidthmultiplier:1constant:0]];//label2高=宽

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label2attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:label1 attribute:NSLayoutAttributeWidthmultiplier:1constant:0]];//label2宽=label1的宽

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label2attribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:label1 attribute:NSLayoutAttributeTopmultiplier:1constant:0]];//label2 label1上边距

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label2attribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:label1 attribute:NSLayoutAttributeTrailingmultiplier:1constant:10]];//label2左距laebl1右边10

    

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label3attribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:label3 attribute:NSLayoutAttributeWidthmultiplier:1constant:0]];//label3高=宽

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label3attribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:label1 attribute:NSLayoutAttributeWidthmultiplier:1constant:0]];//label3宽=label1的宽

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label3attribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:label1 attribute:NSLayoutAttributeTopmultiplier:1constant:0]];//label3 label1上边距

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label3attribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:label2 attribute:NSLayoutAttributeTrailingmultiplier:1constant:10]];////label3左距laebl2右边10

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:label3attribute:NSLayoutAttributeTrailingMarginrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeTrailingMarginmultiplier:1constant:0]];//label3距右边8


         下面是效果图:


  其实呢这个方法虽然好用,但是代码量略多,显得臃肿,下期给大家介绍一个第三方工具Masonry的使用。




0 0