IOS6之AutoLayout(四)

IOS6之AutoLayout(一)

http://blog.csdn.net/zfpp25_/article/details/8861221

IOS6之AutoLayout(二)

http://blog.csdn.net/zfpp25_/article/details/8861855

IOS6之AutoLayout(三)

http://blog.csdn.net/zfpp25_/article/details/8861958

IOS6之AutoLayout(四)

http://blog.csdn.net/zfpp25_/article/details/8862040

IOS6之AutoLayout(五)

http://blog.csdn.net/zfpp25_/article/details/8862157



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


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

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

{

//相对布局代码

} else

{

//绝对布局代码

}


下面看代码:

  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:



NSLayoutFormatDirectionLeftToRight:



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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值