Search code examples
objective-cxcodeconstraintsautoresizingmask

Objective C Constraints in Classes


I'm currently attempting to add a UIIMageView on top of a class. Without constraints, I can add an object such as an UIImageView without any hindrance: [self addSubview:myImage];. However, when I add [myImage setTranslatesAutoresizingMaskIntoConstraints: NO]; along with the needed constraints, the image is added to the UIViewController versus the class.

(Right now, the sizeX/Y is a separate algorithm to set the image width and height and won't be needed assuming constraints will work)

1) Why is that?

2) How can add a UIImageView with constraints?

Here's the code:

UIImageView *myImage = [[UIImageView alloc] init];
//AutoResize...
myImage.frame = CGRectMake(0, 0, sizeX, sizeY);
myImage.center = CGPointMake(placementX, placementY);

myImage.image = [UIImage imageNamed:@"customImage"];

[self addSubview:myImage];

                // Width constraint
 [self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
                                                                 attribute:NSLayoutAttributeWidth
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self
                                                                 attribute:NSLayoutAttributeWidth
                                                                multiplier:0.5
                                                                  constant:0]];

                // Height constraint
 [self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
                                                                 attribute:NSLayoutAttributeHeight
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self
                                                                 attribute:NSLayoutAttributeHeight
                                                                multiplier:0.5
                                                                  constant:0]];

Thanks!


Solution

  • I'm currently attempting to add a UIMageView on top of a class...

    ...the image is added to the view controller versus the class.

    Neither of these statements make sense to me, but...

    Looking at your constraint code, you set constraints for the width and height of the image view, but that is not enough to unambiguously position the image view. You need to add 2 constraints: for the vertical and horizontal position of the image view. For example if you want the image view centered in its superview:

    [self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
                                                                 attribute:NSLayoutAttributeCenterX
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self
                                                                 attribute:NSLayoutAttributeCenterX
                                                                multiplier:1.0
                                                                  constant:0]];
    
             
    [self addConstraint:[NSLayoutConstraint constraintWithItem:myImage
                                                                 attribute:NSLayoutAttributeCenterY
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self
                                                                 attribute:NSLayoutAttributeCenterY
                                                                multiplier:1.0
                                                                  constant:0]];