您的位置:首页 > 移动开发 > IOS开发

关于IOS的Autolayout代码编写使用

2014-05-12 22:12 525 查看
- (void)viewDidLoad

{

    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn1 setTitle:@"Click Me" forState:UIControlStateNormal];

    [btn1 setTranslatesAutoresizingMaskIntoConstraints:NO];//标记是否自动布局

    [self.view addSubview:btn1];

    

    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn2 setTitle:@"Click Me Please" forState:UIControlStateNormal];

    [btn2 setTranslatesAutoresizingMaskIntoConstraints:NO];//标记是否自动布局

    [self.view addSubview:btn2];

    

    NSDictionary *views=NSDictionaryOfVariableBindings(btn1,btn2);

    [self.view addConstraints:

     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(==50)-[btn1(100)]"

                                             options:0

                                             metrics:nil

                                               views:views]];

    [self.view addConstraints:

     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(==50)-[btn1(30)]"

                                             options:0

                                             metrics:nil

                                               views:views]];

    

    [self.view addConstraints:

     [NSLayoutConstraint constraintsWithVisualFormat:@"H:[btn2(==150)]" </span><span style="font-family: 'Comic Sans MS'; "><span style="font-size:12px;">//H=Horizontal,水平方向,同时设定控件宽度</span></span><span style="font-size:14px;">

                                          options:0

                                             metrics:nil

                                               views:views]];

    [self.view addConstraints:

     [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=200)-[btn2(==btn1)]" //V=Vertical,垂直方向,同时设定控件高度

                                             options:0

                                             metrics:nil

                                               views:views] ];

    

    

    //注意AddConstraints和AddConstraint之间的区别,一个添加的参数是(NSArray *),一个是(NSLayoutConstraint *)

    [self.view addConstraint:

     [NSLayoutConstraint constraintWithItem:btn2

                                  attribute:NSLayoutAttributeLeft

                                  relatedBy:NSLayoutRelationEqual

                                     toItem:btn1

                                  attribute:NSLayoutAttributeRight

                                 multiplier:1

                                   constant:10]];

    [self.view addConstraint:

     [NSLayoutConstraint constraintWithItem:btn2

                                  attribute:NSLayoutAttributeTop  //要设定的属性

                                  relatedBy:NSLayoutRelationGreaterThanOrEqual  //大于还是小于相对的View的值

                                     toItem:btn1   //相对于某个View或者控件

                                  attribute:NSLayoutAttributeTop  //指定要设定的关联View的属性

                                 multiplier:1   //因子值

                                   constant:0]];

    

        

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

转载自:http://blog.csdn.net/mobilecode/article/details/8842379
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: