user3661956
user3661956

Reputation: 49

Why isn't my code moving image position?

I want an image to automatically align to the top, I figure the only want to do this is through making a frame that positions it that way. After doing some research, I can't seem to figure out why this code doesn't move the image at all... I have substituted many values in the topPadding and leftPadding variables..

- (void)constructImageView:(UIImage *)image {
    CGFloat topPadding = 20.f;
    CGFloat leftPadding = 30.f;
    CGRect frame = CGRectMake(leftPadding, topPadding, image.size.width, image.size.height);
    self.imageView = [[UIImageView alloc] initWithFrame:frame];
    self.imageView.image = image;
    [self addSubview:self.imageView];
}

Upvotes: 0

Views: 44

Answers (1)

gsach
gsach

Reputation: 5775

Are you using Autolayout? If yes, setFrame: does not work properly with autolayout. If thats the case, then your options are:

  • Use this: self.view.translatesAutoresizingMaskIntoConstraints = YES
  • Write your own constraints instead of setting the view's frame

Upvotes: 1

Related Questions