http://blog.youkuaiyun.com/zfpp25_/article/details/8861221
http://blog.youkuaiyun.com/zfpp25_/article/details/8861855
http://blog.youkuaiyun.com/zfpp25_/article/details/8861958
http://blog.youkuaiyun.com/zfpp25_/article/details/8862040
http://blog.youkuaiyun.com/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.youkuaiyun.com/postedit/8862040
若发现有不合适或错误之处,还请批评指正,不胜感激。
本文详细介绍iOS中AutoLayout的使用方法,包括相对布局的实现,并通过具体示例代码展示了水平和垂直方向上的约束设置。
1451

被折叠的 条评论
为什么被折叠?



