ios NSLayoutConstraint

   为了让我们的应用在不同尺寸的屏幕下都能正常的表示,我们尽量不要把数据写死。大多数可视元素都是一个矩形区域,当然这个矩形区域有坐标的,我们有了这个区域坐标就能确定可视元素的现实位置了。但是iphone5和以前的屏幕不一样了,在以前的设备中,我们可以添加一个 xx.@2x.png来适应retina屏幕,但是iphoen5咋办呢?ios6引入了 Auto Layout的东东,这个要和UIViewAutoresizing区分下。

1.看下面代码:

-(void)viewDidLoad

{

   [superviewDidLoad];   

   UIView *aView =[[UIView alloc] init];

  aView.backgroundColor = [UIColor redColor];

  //为了不和autosizing冲突,我们设置No

  [aView  setTranslatesAutoresizingMaskIntoConstraints:NO];//不把AutoresizingMask转化为Constraints

   [self.view  addSubview:aView];

   UIView *bView =[[UIView alloc] init];

  bView.backgroundColor = [UIColor blueColor];

   [bView  setTranslatesAutoresizingMaskIntoConstraints:NO];

   [self.view  addSubview:bView];

   NSDictionary*views =NSDictionaryOfVariableBindings(aView, bView);

/* This macro is a helper for making view dictionariesfor +constraintsWithVisualFormat:options:metrics:views:. 

 NSDictionaryOfVariableBindings(v1, v2, v3) isequivalent(等效于)  to [NSDictionarydictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3,@"v3", nil];

 */

   [self.viewaddConstraints:

  [NSLayoutConstraint   constraintsWithVisualFormat:@"H:|-(>=50)-[aView(100)]"

                                          options:0

                                          metrics:nil

                                           views:views]];

   [self.view  addConstraints:

   [NSLayoutConstraint  constraintsWithVisualFormat:@"V:|-(>=100)-[aView(50)]"

                                          options:0

                                         metrics:nil

                                           views:views]];

   [self.viewaddConstraints:

    [NSLayoutConstraint   constraintsWithVisualFormat:@"H:[bView(==aView)]"

                                          options:0

                                         metrics:nil

                                           views:views]];

   [self.view addConstraints:

   [NSLayoutConstraint   constraintsWithVisualFormat:@"V:[bView(==aView)]"

                                          options:0

                                         metrics:nil

                                           views:views]];

   [self.viewaddConstraint:

   [NSLayoutConstraint  constraintWithItem:bView

                               attribute:NSLayoutAttributeLeft

                               relatedBy:NSLayoutRelationEqual

                                   toItem:aView

                                attribute:NSLayoutAttributeRight

                               multiplier:1

                                 constant:10]];

   //添加一个限制等效于bView.frame.origin.x = (aView.frame.origin.x +aView.frame.size.width) * 1  + 10!它是一种依赖关系,bView依赖aView,这样就算aView变了,bView也会跟着变换。

   [self.viewaddConstraint:

   [NSLayoutConstraint  constraintWithItem:bView

                               attribute:NSLayoutAttributeTop

                               relatedBy:NSLayoutRelationEqual

                                   toItem:aView

                               attribute:NSLayoutAttributeTop

                               multiplier:1

                                 constant:0]];

   [aView release];

   [bView release];

}

 

 

 

2.constraintWithItem:attribute:relatedBy:toItem:attribute: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.

 */


3.属性

typedefNS_ENUM(NSInteger, NSLayoutAttribute) {

   NSLayoutAttributeLeft = 1,

   NSLayoutAttributeRight,

   NSLayoutAttributeTop,

   NSLayoutAttributeBottom,

   NSLayoutAttributeLeading,

   NSLayoutAttributeTrailing,

   NSLayoutAttributeWidth,

   NSLayoutAttributeHeight,

   NSLayoutAttributeCenterX,

   NSLayoutAttributeCenterY,

   NSLayoutAttributeBaseline,

   NSLayoutAttributeNotAnAttribute = 0

};


4.关系

typedefNS_ENUM(NSInteger, NSLayoutRelation) {

   NSLayoutRelationLessThanOrEqual = -1,

   NSLayoutRelationEqual = 0,

   NSLayoutRelationGreaterThanOrEqual = 1,

};

最后的结果就是 “view1.attr1 <= 或者 ==   或者 >=  view2.attr2 * multiplier + constant”


5.Visual Format Language  (视觉形式语言)

posted @ 2013-12-27 10:32  浪人残风  阅读(354)  评论(0编辑  收藏  举报