How to make a UIImageView, center vertically in container with constraints programmatically

20,895

Solution 1

Try this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    redView.backgroundColor = [UIColor redColor];
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:redView];
    [redView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:100]];
    [redView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:200]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self.view addSubview:redView];
}

Solution 2

Try

self.showImageView.center=self.view.center;

This will do your work by setting self.showImageView to the center of self.view.

Share:
20,895
iTamilan
Author by

iTamilan

Updated on July 04, 2020

Comments

  • iTamilan
    iTamilan almost 4 years

    I want to center UIImageView (self.showImageView) in container view (self.view)

    I tried below code it did't work:

    NSLayoutConstraint *xCenterConstraint = [NSLayoutConstraint constraintWithItem:self.showImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    [self.view addConstraint:xCenterConstraint];
    
    NSLayoutConstraint *yCenterConstraint = [NSLayoutConstraint constraintWithItem:self.showImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    [self.view addConstraint:yCenterConstraint];
    
  • Seth Jeffery
    Seth Jeffery over 8 years
    translatesAutoresizingMaskIntoConstraints was the solution for me. In Swift, it's setTranslatesAutoresizingMaskIntoConstraints
  • user3144836
    user3144836 almost 8 years
    plus one for simplicity
  • Prabu Arumugam
    Prabu Arumugam over 7 years
    This will not crash with error "view hierarchy is not prepared for the constraint" ? You might need to add the constraints to superviews instead of a child view. According to Apple docs: When added to a view, the constraint's items must be descendants of that view (or the view itself).
  • Vitalii Gozhenko
    Vitalii Gozhenko over 7 years
    @PrabuArumugam this is depends on constraint type. Some of them (height/width) you need to add to view, not superview