码迷,mamicode.com
首页 > 移动开发 > 详细

iOS动态管理AutoLayout的约束NSLayoutConstraint

时间:2015-10-23 18:29:46      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:

除了使用Storyboard之外,也可以使用使用代码的方式,动态的向指定的UIView,添加约束。

例如有两个UILabel:someLabel,otherLabel

首先用代码实例化,两个控件

self.someLabel = [[UILabel alloc]initWithFrame:CGRectZero];
self.someLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.inputContainerView];

self.otherLabel = [[UILabel alloc]initWithFrame:CGRectZero];
self.otherLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.inputContainerView];

两点很重要:

  1. translatesAutoresizingMaskIntoConstraints = NO  否则布局就会混乱
  2. someLabel和otherLabel必须有superView,否则就会出现出现警告

The view hierarchy is not prepared for the constraint

When added to a view, the constraint‘s items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled.

运行时候Crash

Terminating app due to uncaught exception ‘NSGenericException‘, reason: ‘Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That‘s illegal

 

例如新建了一个UIView的Category

其中一个方法

-(NSLayoutConstraint*)verticalDistanceTop:(UIView*)topView constant:(CGFloat)constant
{
    NSLayoutConstraint*topConstrant = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:topView     attribute:NSLayoutAttributeBottom multiplier:1 constant:constant];
    return topConstrant;
}

返回一个约束,保证self具体topView的底部距离为contant

约束的构造函数 :constraintWithItem,注意计算公式:

view1.attr1 = view2.attr2 * multiplier + constant

/* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant" 
 If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
 */
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

生成约束后,使其生效的方法

 NSLayoutConstraint*topConstraint = [self.someLabel verticalDistanceTop:self.otherLabel constant:22];
    topConstraint.active = YES;

 

iOS动态管理AutoLayout的约束NSLayoutConstraint

标签:

原文地址:http://www.cnblogs.com/taojigu/p/4905114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!