手寫代碼佈局例子

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layout];


}
-(void)layout{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor blackColor];
    UIView *view1 = [[UIView alloc]init];
    view1.backgroundColor = [UIColor redColor];
    UIView *view2 = [[UIView alloc]init];
    view2.backgroundColor = [UIColor yellowColor];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    view2.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:view];
    [self.view addSubview:view1];
    [self.view addSubview:view2];
    NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)];
    NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view1]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view1)];
    NSArray *constraints3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[view][view1(==view)][view2(==view1)]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view,view1,view2)];
     NSArray *constraints4 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view2]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view2)];
    [self.view addConstraints:constraints1];
    [self.view addConstraints:constraints2];
    [self.view addConstraints:constraints3];
    [self.view addConstraints:constraints4];
   }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
相關文章
相關標籤/搜索