Autolayout(自動佈局)

VFL語言實現如下界面:css

 

- (void)viewDidLoadurl

{orm

    [super viewDidLoad];blog

    //建立上面的viewip

    UIView *topView = [[UIView alloc]init];it

    topView.backgroundColor = [UIColor redColor];io

    [self.view addSubview:topView];import

    topView.translatesAutoresizingMaskIntoConstraints = NO;file

    //建立下面的viewim

    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];

}

相關文章
相關標籤/搜索