IOS6之AutoLayout(四)
来源:互联网 发布:站外优化的理解 编辑:程序博客网 时间:2024/05/29 02:38
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
{
//绝对布局代码
}
下面看代码:
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- if ([self.viewrespondsToSelector:@selector(addConstraints:)])
- {
- [self newStyle];
- } else
- {
- [self oldStyle];
- }
- }
- - (void) oldStyle
- {
- UIView *myView = [[UIViewalloc]init];
- myView.backgroundColor = [UIColorgreenColor];
- myView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
- UIView *redView = [[UIViewalloc]init];
- redView.backgroundColor = [UIColorredColor];
- redView.frame = CGRectMake(50,44,100,30);
- UIView *blueView = [[UIViewalloc]init];
- blueView.backgroundColor = [UIColorblueColor];
- blueView.frame = CGRectMake(180,44,100,30);
- [myView addSubview:redView];
- [myView addSubview:blueView];
- self.view = myView;
- }
- - (void) newStyle
- {
- UIView *myView = [[UIViewalloc]init];
- myView.backgroundColor = [UIColorwhiteColor];
- UIView *redView = [[UIViewalloc]init];
- redView.backgroundColor = [UIColorredColor];
- UIView *blueView = [[UIViewalloc]init];
- blueView.backgroundColor = [UIColorblueColor];
- [myView addSubview:redView];
- [myView addSubview:blueView];
- [myView setTranslatesAutoresizingMaskIntoConstraints:NO];
- [redView setTranslatesAutoresizingMaskIntoConstraints:NO];
- [blueView setTranslatesAutoresizingMaskIntoConstraints:NO];
- NSMutableArray *tmpConstraints = [NSMutableArrayarray];
- // 水平方向布局
- [tmpConstraints addObjectsFromArray:
- [NSLayoutConstraint constraintsWithVisualFormat:@"|-50-[redView(==100)]-30-[blueView(==100)]"
- options:NSLayoutFormatDirectionLeadingToTrailing
- metrics:nil
- views:NSDictionaryOfVariableBindings(redView,blueView)]];
- // 垂直方向布局
- [tmpConstraints addObjectsFromArray:
- [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-44-[redView(==30)]"
- options:NSLayoutFormatDirectionLeadingToTrailing
- metrics:nil
- views:NSDictionaryOfVariableBindings(redView)]];
- [tmpConstraints addObjectsFromArray:
- [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-44-[blueView(==redView)]"
- options:NSLayoutFormatDirectionLeadingToTrailing
- metrics:nil
- views:NSDictionaryOfVariableBindings(blueView,redView)]];
- [myView addConstraints:tmpConstraints];
- // 按钮布局
- UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
- [button setTitle:@"ssssss"forState:UIControlStateNormal];
- [button setTranslatesAutoresizingMaskIntoConstraints:NO];
- [myView addSubview:button];
- NSLayoutConstraint *lc = [NSLayoutConstraint
- constraintWithItem:button
- attribute:NSLayoutAttributeBottom
- relatedBy:NSLayoutRelationEqual
- toItem:myView
- attribute:NSLayoutAttributeBottom
- multiplier:1.0
- constant:-10];
- [myView addConstraint:lc];
- lc = [NSLayoutConstraint
- constraintWithItem:button
- attribute:NSLayoutAttributeCenterX
- relatedBy:NSLayoutRelationEqual
- toItem:myView
- attribute:NSLayoutAttributeCenterX
- multiplier:1.0
- constant:0];
- [myView addConstraint:lc];
- self.view = myView;
- }
下面来讲解核心代码:
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
若发现有不合适或错误之处,还请批评指正,不胜感激。
- IOS6之AutoLayout(四)
- IOS6之AutoLayout(四)
- IOS6之AutoLayout(四)
- IOS6之AutoLayout(一)
- IOS6之AutoLayout(二)
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(五)
- IOS6之AutoLayout(一)
- IOS6之AutoLayout(二)
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(一)
- IOS6之AutoLayout(二)
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(五)
- ios6 autoLayout
- AutoLayout(四)
- AutoLayout在iOS6和Xcode5中的应用
- 【IOS6.0 自学瞎折腾】(四)Xib可视化编程
- 邮箱 POP3服务器(端口110) SMTP服务器(端口25)
- 数字证书
- IOS6之AutoLayout(三)
- 【Object-C】类Class
- JVM垃圾回收与内存分配(一)
- IOS6之AutoLayout(四)
- MFC消息映射与命令传递
- 如何查询Eclipse版本号
- 自己动手写一个轻巧,高效的正则表达式引擎
- IOS6之AutoLayout(五)
- 第14周项目6.3
- 递归算法
- android SDK如何进行离线安装
- liyuyu.cn-呵呵小鱼的个人博客平台