IOS6之AutoLayout(四)

这一篇讲解更通用的相对布局方法,其中例子引用别人的一个demo。


IOS的UIView是否可以使用相对布局,可以用如下方法去判断:

if ([self.viewrespondsToSelector:@selector(addConstraints:)])

{

//相对布局代码

} else

{

//绝对布局代码

}


下面看代码:

[cpp]  view plain copy print ?
  1. - (void)viewDidLoad  
  2. {  
  3.     [superviewDidLoad];  
  4.       
  5.     if ([self.viewrespondsToSelector:@selector(addConstraints:)])  
  6.     {  
  7.         [self newStyle];  
  8.     } else  
  9.     {  
  10.         [self oldStyle];  
  11.     }  
  12. }  
  13.   
  14. - (void) oldStyle  
  15. {  
  16.     UIView *myView = [[UIViewalloc]init];  
  17.     myView.backgroundColor = [UIColorgreenColor];  
  18.     myView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);  
  19.       
  20.     UIView *redView = [[UIViewalloc]init];  
  21.     redView.backgroundColor = [UIColorredColor];  
  22.     redView.frame = CGRectMake(50,44,100,30);  
  23.       
  24.     UIView *blueView = [[UIViewalloc]init];  
  25.     blueView.backgroundColor = [UIColorblueColor];  
  26.     blueView.frame = CGRectMake(180,44,100,30);  
  27.       
  28.     [myView addSubview:redView];  
  29.     [myView addSubview:blueView];  
  30.       
  31.     self.view = myView;  
  32. }  
  33.   
  34. - (void) newStyle  
  35. {  
  36.     UIView *myView = [[UIViewalloc]init];  
  37.     myView.backgroundColor = [UIColorwhiteColor];  
  38.       
  39.     UIView *redView = [[UIViewalloc]init];  
  40.     redView.backgroundColor = [UIColorredColor];  
  41.       
  42.     UIView *blueView = [[UIViewalloc]init];  
  43.     blueView.backgroundColor = [UIColorblueColor];  
  44.       
  45.     [myView addSubview:redView];  
  46.     [myView addSubview:blueView];  
  47.       
  48.     [myView setTranslatesAutoresizingMaskIntoConstraints:NO];  
  49.     [redView setTranslatesAutoresizingMaskIntoConstraints:NO];  
  50.     [blueView setTranslatesAutoresizingMaskIntoConstraints:NO];  
  51.       
  52.     NSMutableArray *tmpConstraints = [NSMutableArrayarray];  
  53.     
  54. //    水平方向布局  
  55.     [tmpConstraints addObjectsFromArray:  
  56.      [NSLayoutConstraint constraintsWithVisualFormat:@"|-50-[redView(==100)]-30-[blueView(==100)]"  
  57.                                                                                options:NSLayoutFormatDirectionLeadingToTrailing  
  58.                                                                                metrics:nil  
  59.                                                                                  views:NSDictionaryOfVariableBindings(redView,blueView)]];  
  60.       
  61.      
  62. //    垂直方向布局  
  63.     [tmpConstraints addObjectsFromArray:  
  64.      [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-44-[redView(==30)]"  
  65.                                              options:NSLayoutFormatDirectionLeadingToTrailing  
  66.                                              metrics:nil  
  67.                                                views:NSDictionaryOfVariableBindings(redView)]];  
  68.      
  69.     [tmpConstraints addObjectsFromArray:  
  70.      [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-44-[blueView(==redView)]"  
  71.                                              options:NSLayoutFormatDirectionLeadingToTrailing  
  72.                                              metrics:nil  
  73.                                                views:NSDictionaryOfVariableBindings(blueView,redView)]];  
  74.     [myView addConstraints:tmpConstraints];  
  75.     
  76.       
  77.       
  78. //    按钮布局  
  79.     UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];  
  80.     [button setTitle:@"ssssss"forState:UIControlStateNormal];  
  81.     [button setTranslatesAutoresizingMaskIntoConstraints:NO];  
  82.     [myView addSubview:button];  
  83.       
  84.     NSLayoutConstraint *lc = [NSLayoutConstraint  
  85.                               constraintWithItem:button  
  86.                               attribute:NSLayoutAttributeBottom  
  87.                               relatedBy:NSLayoutRelationEqual  
  88.                               toItem:myView  
  89.                               attribute:NSLayoutAttributeBottom  
  90.                               multiplier:1.0  
  91.                               constant:-10];  
  92.     [myView addConstraint:lc];  
  93.       
  94.     lc = [NSLayoutConstraint  
  95.           constraintWithItem:button  
  96.           attribute:NSLayoutAttributeCenterX  
  97.           relatedBy:NSLayoutRelationEqual  
  98.           toItem:myView  
  99.           attribute:NSLayoutAttributeCenterX  
  100.           multiplier:1.0  
  101.           constant:0];  
  102.     [myView addConstraint:lc];  
  103.       
  104.     self.view = myView;  
  105. }  



下面来讲解核心代码:

1、"|-50-[redView(==100)]-30-[blueView(==100)]" 如何理解?

屏幕左边沿 -> 空50距离 ->  redView(其redView宽度是100)-> 空30距离 -> blueView(其blueView宽度为100)


2、"V:|-44-[redView(==30)]"
 道理同 1,只不过从垂直方向看起,这里不赘述了。


3、NSLayoutFormatDirectionLeadingToTrailing 怎么理解?

NSLayoutFormatDirectionLeadingToTrailing

Arrange objects in order based on the normal text flow for the current user interface language. In English this results in the first object being placed farthest to the left, the next one to its right, and so on. In right to left languages this ordering is reversed.

意思就是默认的排版方式,就是从左往右看,从上往下看。

于是就引出了:

NSLayoutFormatDirectionLeftToRight  与 NSLayoutFormatDirectionRightToLeft

也不难理解,前一个是从屏幕左沿布局开始算起,后一个是从屏幕右沿开始算起。


下面看效果图:

NSLayoutFormatDirectionRightToLeft:

IOS6之AutoLayout(四)_第1张图片


NSLayoutFormatDirectionLeftToRight:

IOS6之AutoLayout(四)_第2张图片



转载请保留,原文链接:http://write.blog.csdn.net/postedit/8862040

若发现有不合适或错误之处,还请批评指正,不胜感激。

你可能感兴趣的:(IOS6之AutoLayout(四))