首页 > 代码库 > Autolayout(自动布局)
Autolayout(自动布局)
VFL语言实现以下界面:
- (void)viewDidLoad
{
[super viewDidLoad];
//创建上面的view
UIView *topView = [[UIView alloc]init];
topView.backgroundColor = [UIColor redColor];
[self.view addSubview:topView];
topView.translatesAutoresizingMaskIntoConstraints = NO;
//创建下面的view
UIView *bottomView = [[UIView alloc]init];
bottomView.backgroundColor = [UIColor blueColor];
[self.view addSubview:bottomView];
bottomView.translatesAutoresizingMaskIntoConstraints = NO;
//利用VFL语言
//创建约束(垂直方向)
NSInteger gap = 20;
NSArray *verticalC = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-gap-[topView(==50)]-gap-[bottomView(==topView)]" options:NSLayoutFormatAlignAllRight metrics:@{@"gap":@(gap)} views:@{@"topView":topView,@"bottomView":bottomView}];
[self.view addConstraints:verticalC];
NSArray *herizonTopC = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-gap-[topView]-gap-|" options:NSLayoutFormatAlignAllRight metrics:@{@"gap":@(gap)} views:@{@"topView":topView}];
[self.view addConstraints:herizonTopC];
NSLayoutConstraint *contraintBottom = [NSLayoutConstraint constraintWithItem:bottomView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:topView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
[self.view addConstraint:contraintBottom];
}
Autolayout(自动布局)