码迷,mamicode.com
首页 > 移动开发 > 详细

IOS手动添加的View 在代码中使用(自动布局)autoLayout

时间:2014-11-01 16:12:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:blog   io   color   os   使用   div   on   log   代码   

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom];//不需要去刻意指定x,y的坐标,可以用CGRectZero
    btnTest.backgroundColor = [UIColor redColor];
    btnTest.layer.borderColor = [UIColor yellowColor].CGColor;
    btnTest.layer.borderWidth = 2.0;
    [self.view addSubview:btnTest];
    
    
    [btnTest setTranslatesAutoresizingMaskIntoConstraints:NO];//将使用AutoLayout的方式布局
    //btnTest顶部相对于self.view的顶部距离为100
    NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];
    //btnTest左侧相对于self.view的左侧距离为100
    NSLayoutConstraint *constraintLeft = [NSLayoutConstraint constraintWithItem:btnTest  attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100];
    //btnTest右侧相对于self.view的右侧距离为100
    NSLayoutConstraint *constraintRight = [NSLayoutConstraint constraintWithItem:self.view  attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:btnTest attribute:NSLayoutAttributeRight multiplier:1.0 constant:100];
    //btnTest底部相对于self.view的底部距离为100
    NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:btnTest attribute:NSLayoutAttributeBottom multiplier:1.0 constant:100];
    //水平居中
    NSLayoutConstraint *constraintXCenter = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:00.0f];
    //垂直居中
     NSLayoutConstraint *constraintYCenter = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:00.0f];
    
    //将约束添加到父视图中
    [self.view addConstraint:constraintTop];
    [self.view addConstraint:constraintLeft];
    [self.view addConstraint:constraintRight];
    [self.view addConstraint:constraintBottom];
    [self.view addConstraint:constraintXCenter];
    [self.view addConstraint:constraintYCenter];
    
}

  

IOS手动添加的View 在代码中使用(自动布局)autoLayout

标签:blog   io   color   os   使用   div   on   log   代码   

原文地址:http://www.cnblogs.com/mgbert/p/4067178.html

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