UIView里面有一个UIImageView我现在想让UIVIew适配屏幕大小
NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:self.imageCropper attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        
        NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:self.imageCropper attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        
        NSLayoutConstraint *constraintLeft = [NSLayoutConstraint constraintWithItem:self.imageCropper attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        
        NSLayoutConstraint *constraintRight = [NSLayoutConstraint constraintWithItem:self.imageCropper attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0];
但是UIView不是在中间
setTranslatesAutoresizingMaskIntoConstraints:NO已经设置求解答

解决方案 »

  1.   

    对于单个控件相对于其父视图的布局你直接用autoresizingMask属性就行了。不需要用autoLayout布局。如果真想用,可以去掉底部和右边的约束条件,改为宽高的约束试试
      

  2.   

    - (void)addContraints
    {
        NSLayoutConstraint *constraints = [NSLayoutConstraint constraintWithItem:view
                                                                       attribute:NSLayoutAttributeTop
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:self.view
                                                                       attribute:NSLayoutAttributeTop
                                                                      multiplier:1.0
                                                                        constant:0];
        [self.view addConstraint:constraints];
        
        constraints = [NSLayoutConstraint constraintWithItem:view
                                                   attribute:NSLayoutAttributeWidth
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:self.view
                                                   attribute:NSLayoutAttributeWidth
                                                  multiplier:1.0
                                                    constant:0];
        [self.view addConstraint:constraints];
        
        constraints = [NSLayoutConstraint constraintWithItem:view
                                                   attribute:NSLayoutAttributeHeight
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:self.view
                                                   attribute:NSLayoutAttributeHeight
                                                  multiplier:1.0
                                                    constant:0];
        [self.view addConstraint:constraints];
        
        constraints = [NSLayoutConstraint constraintWithItem:view
                                                   attribute:NSLayoutAttributeLeft
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:self.view
                                                   attribute:NSLayoutAttributeLeft
                                                  multiplier:1.0
                                                    constant:0];
        [self.view addConstraint:constraints];
    }
    这样写就行了。