IOS6之AutoLayout(四)
来源:互联网 发布:feel drcs软件 编辑:程序博客网 时间:2024/05/21 06:46
这一篇讲解更通用的相对布局方法,其中例子引用别人的一个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可视化编程
- 将某个Qt4项目升级到Qt5遇到的问题
- jQuery源码分析-13 CSS操作-CSS-类样式-addClass+removeClass+toggleClass+hasClass
- UVA 10452 Marcus, help!( DFS )
- 解读Oracle执行计划
- Ajax中Get和Post两种传值方式的异同
- IOS6之AutoLayout(四)
- What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?
- java使用Runtime.exec执行(本地)linux/Windows命令
- jQuery源码分析-15AJAX-前置过滤器和请求分发器
- NSString 一些常用用法
- android 编译出错问题
- 使用非托管 DLL 函数[C#]
- Android4.4 SkImageDecoder_libgif.cpp 使用 giflib5.0.4 的 bug
- thinkphp引入phpqrcode 生成二维码