码迷,mamicode.com
首页 > 其他好文 > 详细

Autolayout(自动布局)

时间:2014-09-22 02:33:11      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:color   io   ar   for   sp   on   c   ad   r   

VFL语言实现以下界面:

bubuko.com,布布扣

- (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(自动布局)

标签:color   io   ar   for   sp   on   c   ad   r   

原文地址:http://www.cnblogs.com/bluceZ/p/3985149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!