美文网首页布局乔小乔的技术收藏
translatesAutoresizingMaskIntoCo

translatesAutoresizingMaskIntoCo

作者: json_jie | 来源:发表于2015-03-11 11:43 被阅读15318次

    (1)
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
    view1.backgroundColor = [UIColor redColor];
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:view1];

       [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
       [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
    
    
    //
    //
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100,200 , 33, 33)];
        view2.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:view2];
        view2.translatesAutoresizingMaskIntoConstraints = NO;
    
    //    // 宽度  高度
        NSLayoutConstraint *heightConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier:1  constant:0];
        // 宽度
        NSLayoutConstraint *widthConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
        toItem:view1 attribute:NSLayoutAttributeWidth  multiplier:1 constant:0];
    
        NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
        NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1 constant:40];
    
        [self.view addConstraints:@[heightConstrait,widthConstrait]];
        [self.view addConstraint:constraintTop];
        [self.view addConstraint:constraintBottom];
    

    (2)

     UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
        view1.backgroundColor = [UIColor redColor];
        view1.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:view1];
    
    
       [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
    //   [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
    
    
    //
    //
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100,200 , 33, 33)];
        view2.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:view2];
        view2.translatesAutoresizingMaskIntoConstraints = NO;
    
    //    // 宽度  高度
        NSLayoutConstraint *heightConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier:1  constant:0];
        // 宽度
        NSLayoutConstraint *widthConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
        toItem:view1 attribute:NSLayoutAttributeWidth  multiplier:1 constant:0];
    
        NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
        NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1 constant:40];
    
        [self.view addConstraints:@[heightConstrait,widthConstrait]];
        [self.view addConstraint:constraintTop];
        [self.view addConstraint:constraintBottom];
    

    (3)
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
    view1.backgroundColor = [UIColor redColor];
    view1.translatesAutoresizingMaskIntoConstraints = YES;
    [self.view addSubview:view1];

       [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
       [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
    
    
    //
    //
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100,200 , 33, 33)];
        view2.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:view2];
        view2.translatesAutoresizingMaskIntoConstraints = NO;
    
    //    // 宽度  高度
        NSLayoutConstraint *heightConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier:1  constant:0];
        // 宽度
        NSLayoutConstraint *widthConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
        toItem:view1 attribute:NSLayoutAttributeWidth  multiplier:1 constant:0];
    
        NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
        NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1 constant:40];
    
        [self.view addConstraints:@[heightConstrait,widthConstrait]];
        [self.view addConstraint:constraintTop];
        [self.view addConstraint:constraintBottom];
    

    translatesAutoresizingMaskIntoConstraints 约束与Autosizing控制。。。
    当打开约束的时候,要约束条件完全,否则可能试图丢失。。。。。

    相关文章

      网友评论

      • aydwb:[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
        [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
        这个不应该是self.view addConstraint 么
        979e521182b4:写的什么啊?
        json_jie:@aydwb 看他的父类了

      本文标题:translatesAutoresizingMaskIntoCo

      本文链接:https://www.haomeiwen.com/subject/pgxbxttx.html