iOS自动布局之通过代码添加约束

有的时候,需要我们通过代码手动添加一些控件的约束,怎么做呢

假使我们已经创建好了某个空间的constant约束,比如下图中鼠标选中的这个

选中他,然后拖动到右边

这样 就和.m文件关联起来

打开.m函数,检查这个函数

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

   if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {

       self.LW_BottomView.constant =50;

    }else{

       self.LW_BottomView.constant =200;

    }

}

这样我们就通过判断设备的方向  来更新这个高度约束的常量属性了(后面会持续更新...)


下面再写一个简单的UIButon做为示例:

//1.先创建一个Button类实例对象btn 并对其做一些简单的设置

做完了这一步 就创建好了一个button对象Btn 设置了标题和背景颜色,并将其添加到了当前view中

UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn

     setTitle:@"LWBtn"forState:UIControlStateNormal];

    [btn setBackgroundColor:[UIColorredColor]];

    [self.viewaddSubview:btn];


//2.下面要对这个btn做一些约束了

 /*将使用autolayout方式来布局*/

    btn.translatesAutoresizingMaskIntoConstraints =NO;


/*居中显示*/

    [self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:btn attribute:NSLayoutAttributeCenterXrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeCenterXmultiplier:1constant:0]];


通过self.view addConstraint这个方法来添加约束,上面的参数的意思分别是:

constraintWithItem:第一个对象btn(view1)

attribute:第一个view的属性attr1

relatedBy:指定左右两个view之间的关系relation

toItem:指定约束右边的view2

attribute:指定第二个view2的attribute属性attr2

multiplier:指定一个与view2属性相乘的乘数 这里设置为1

constant:指定一个与view2属性相加的浮点数 这里设置为0

这个函数的对照公式为:view1.attr1 <relation> view2.attr2*multiplier+constant

注意:如果你想设置的约束里不需要view2,需要将第4个参数设置为nil,并且第5个参数设置为NSLayoutAttributeNotAnAttribute


再举个例子:

[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft

relatedBy:NSLayoutRelationEqual  toItem:view2 attribute:NSLayoutAttributeRight multiplier:1 constant:10]

这个约束的含义就是view1的左侧在view2右侧还要多10个点的地方


视图的属性和关系的值:

typedefNS_ENUM(NSInteger, NSLayoutRelation) {

    NSLayoutRelationLessThanOrEqual = -1,//小于等于

    NSLayoutRelationEqual =0,//等于

    NSLayoutRelationGreaterThanOrEqual =1,//大于等于

};


typedefNS_ENUM(NSInteger, NSLayoutAttribute) {

    NSLayoutAttributeLeft =1,//左侧

    NSLayoutAttributeRight,//右侧

    NSLayoutAttributeTop,//上方

    NSLayoutAttributeBottom,//下方

    NSLayoutAttributeLeading,//首部

    NSLayoutAttributeTrailing,//尾部

    NSLayoutAttributeWidth,//宽度

    NSLayoutAttributeHeight,//高度

    NSLayoutAttributeCenterX,//X轴中心

    NSLayoutAttributeCenterY,//Y轴中心

    NSLayoutAttributeBaseline,//文本底标线

    NSLayoutAttributeLastBaseline =NSLayoutAttributeBaseline,

    NSLayoutAttributeFirstBaselineNS_ENUM_AVAILABLE_IOS(8_0),

    

    

    NSLayoutAttributeLeftMarginNS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeRightMarginNS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeTopMarginNS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeBottomMarginNS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeLeadingMarginNS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeTrailingMarginNS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeCenterXWithinMarginsNS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeCenterYWithinMarginsNS_ENUM_AVAILABLE_IOS(8_0),

    

   NSLayoutAttributeNotAnttribute =0//没有属性

};


下面再来个简单的例子:

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn

     setTitle:@"LWBtn" forState:UIControlStateNormal];

    [btn setBackgroundColor:[UIColor greenColor]];

    [self.view addSubview:btn];

    /*将使用autolayout方式来布局*/

    btn.translatesAutoresizingMaskIntoConstraints = NO;

    /*居中显示*/

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];

    /*距离底部20单位*/

    [self.view  addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:-20]];

    /*定义高度是父view1/3*/

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.3 constant:0]];

  NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeHeight relatedBy:

   NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.3 constant:0];

    /*设置了优先级*/

    con.priority = UILayoutPriorityDefaultHigh;

    [self.view addConstraint:con];

    

    /*设置btn最小高度为150*/

    [btn addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]];

    

    

    /*注册KVO方法Key-Value-Observing*/

    [btn addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew context:nil];

}



//KVO回调

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    [btn setTitle:NSStringFromCGSize(btn.bounds.size) forState:UIControlStateNormal];

    NSLog(@"改变的属性:%@",keyPath);

}


下面是几片推荐的文章,写的不错 

http://blog.csdn.net/zhuzhihai1988/article/details/43926055 

http://doc.okbase.net/u013263917/archive/121406.html



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值