AutoLayout

// ラベル中央表示

    UILabel *centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    centerLabel.textColor = [UIColor whiteColor];
    centerLabel.text = @"centerLabel";
    
    [centerLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

    NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:centerLabel
                                                                   attribute:NSLayoutAttributeCenterX
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeCenterX
                                                                  multiplier:1
                                                                    constant:0];
    
    NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:centerLabel
                                                                   attribute:NSLayoutAttributeCenterY
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeCenterY
                                                                  multiplier:1
                                                                    constant:0];
    
    [self.view addSubview:centerLabel];
    [self.view addConstraint:xConstraint];
    [self.view addConstraint:yConstraint];



// 4角固定

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    view.backgroundColor = [UIColor redColor];
    view.alpha = 0.2f;
    [self.view addSubview:view];
    
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.view addConstraints:@[
                                
                                [NSLayoutConstraint constraintWithItem:view
                                                             attribute:NSLayoutAttributeTop
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeTop
                                                            multiplier:1.0
                                                              constant:padding.top],
                                
                                [NSLayoutConstraint constraintWithItem:view
                                                             attribute:NSLayoutAttributeLeft
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeLeft
                                                            multiplier:1.0
                                                              constant:padding.left],
                                
                                [NSLayoutConstraint constraintWithItem:view
                                                             attribute:NSLayoutAttributeBottom
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeBottom
                                                            multiplier:1.0
                                                              constant:-padding.bottom],
                                
                                [NSLayoutConstraint constraintWithItem:view
                                                             attribute:NSLayoutAttributeRight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeRight
                                                            multiplier:1
                                                              constant:-padding.right]

                                ]];
最終更新:2016年07月26日 14:27