objective-c 蘋果官方NSLayoutConstraint 頁面佈局

//佈局

//  ViewController.mspa

//  代碼佈局約束3d

//orm

//  Created by DC017 on 16/1/4.it

//  Copyright © 2016 DC017. All rights reserved.io

//test

//蘋果官方NSLayoutConstraintimport


#import "ViewController.h"im


@interface ViewController ()margin


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

//    [self test1];

//    [self test2];

    [self test3];

    

    

}

-(void)test1{

    UIButton * button=[[UIButton alloc]init];

    [button setTitle:@"按鈕" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button.backgroundColor=[UIColor orangeColor];

    [self.view addSubview:button];

    //必需要作的  禁止自動轉化AutoresizingMask

    button.translatesAutoresizingMaskIntoConstraints=NO;

    //建立佈局約束

    NSArray * constraints1=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[button]-100-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)];

    

    //NSDictionaryOfVariableBindings(button);

    

    NSArray * constraints2=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[button(100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)];

    [self.view addConstraints:constraints1];

    [self.view addConstraints:constraints2];

    

    

    

    

    

}

-(void)test2{

    

    UIButton * button2=[[UIButton alloc]init];

    [button2 setTitle:@"按鈕" forState:UIControlStateNormal];

    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button2.backgroundColor=[UIColor purpleColor];

    [self.view addSubview:button2];

    

    //必需要有的否則顯示不出來

    button2.translatesAutoresizingMaskIntoConstraints=NO;

    //H:|-100-[button2]-100-| --->對應意思:H表明水平 第一個|表明屏幕左邊的邊框 第二個|表明屏幕右邊邊框 |-100[button]-100-|表明button距屏幕左右都有100的距離

    //V:|-200-[button2(100)] ---->對應意思:V表明垂直 |-200[button(200)]-|表明距離屏幕頂部距離是200button的寬度是button(200)

        NSArray * constraints3=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-100-[button2]-100-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button2)];

        NSArray * constraints4=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-200-[button2(100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button2)];

        [self.view addConstraints:constraints3];

        [self.view addConstraints:constraints4];

        

    


}

-(void)test3{

    UIView * view1=[[UIView alloc]init];

    UIView * view2=[[UIView alloc]init];

    UIView * view3=[[UIView alloc]init];

    

    view1.backgroundColor=[UIColor redColor];

    view2.backgroundColor=[UIColor orangeColor];

    view3.backgroundColor=[UIColor purpleColor];

    

    view1.translatesAutoresizingMaskIntoConstraints=NO;

    view2.translatesAutoresizingMaskIntoConstraints=NO;

    view3.translatesAutoresizingMaskIntoConstraints=NO;

    

    [self.view addSubview:view1];

    [self.view addSubview:view2];

    [self.view addSubview:view3];

    

    //建立約束

    NSArray * constraintsI=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view1]-10-[view2(==view1)]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view1,view2)];

    NSArray * constraintsII=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[view2]-10-[view3(==view2)]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view2,view3)];


    NSArray * constraintsIIII=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view3]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view3)];

    NSArray * constraintsIIIII=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[view1]-10-[view3]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view1,view3)];


    

    [self.view addConstraints:constraintsI];

    [self.view addConstraints:constraintsII];

    [self.view addConstraints:constraintsIIII];

    [self.view addConstraints:constraintsIIIII];

   

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相關文章
相關標籤/搜索