0

I'm trying to add an Image to my view. I tried the following code:

  UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tag40pixelsborder.png"]];
    imgView.frame = CGRectMake(200, 200, 30, 30);
//    imgView.bounds = CGRectMake(300, 300, 32, 32);
    [imgView setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:imgView];


    NSLog(@"frame x %f", imgView.frame.origin.x);
    NSLog(@"frame y %f", imgView.frame.origin.y);

But the picture doesn't appear on screen, and NSLog return 30.00 for "x" and 0.00 for "y" If I uncomment the bounds line and I remove the frame line, I get 80.00 for "x" and 80.00 for "y".

I am using autolayout, but the exact same code works on another view! What's wrong?

8
  • what are the dimensions of your image? Commented Dec 11, 2013 at 14:31
  • That code works for me, Ve you added image to xcode check the spelling of image Commented Dec 11, 2013 at 14:34
  • I copied-paste the name of the image, I'm sure it's the good one. Commented Dec 11, 2013 at 14:35
  • That's the thing. The image is 160x160, but I want to resize it. There's a typo, it should be 32x32 so I downsize it at a 5 to 1 ratio. Commented Dec 11, 2013 at 14:35
  • If you insist on using autoLayout, how about adding the ImageView inside IB but then setting image at runtime through the code? Commented Dec 11, 2013 at 14:47

2 Answers 2

1

If you wish to use auto layout to do this then you should do something along these lines.

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tag40pixelsborder.png"]];
imgView.translatesAutoresizingMaskIntoConstraints = NO;
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView sizeToFit];
[self.view addSubview:imgView];

Then you will want to set the auto layout constraints what i see you have is x = 200 y = 200

NSArray *constraints = [NSLayoutConstraint 
                        constraintsWithVisualFormat:@"V:|-(200)-[imgView]"
                        options:0
                        metrics:nil
                        views:@{ @"imgView" : imgView }];
[self.view addConstraints:constraints];

constraints = [NSLayoutConstraint 
               constraintsWithVisualFormat:@"|-(200)-[imgView]"
               options:0
               metrics:nil
               views:@{ @"imgView" : imgView }]; 
[self.view addConstraints:constraints];  

The above will make the image view 200 from the top and 200 from the left of the parent view i.e. self.view in this case.

If you wanted to add the heights you could remove the [imgView sizeToFit] and just add heights to the width and height constraints i.e. |-(200)-[imgView(width)] and same for the height in the one marked with the V:.

Sign up to request clarification or add additional context in comments.

10 Comments

I tried your solution, but I get "No known class method for selector 'constraintsWithVisualFormat:options:metrics:views' " from Xcode. Did I forget to import something?
However the image now shows up (with your first 5 lines of code), but it in in original resolution (160x160) and whenever I want to change the frame/bounds, it disappears.
should have been constraintsWithVisualFormat not constraintWithVisualFormat try that Xcode should not cause an issue now
Error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: Unable to interpret '|' character, because the related view doesn't have a superview V:|-(200)-[imgView]
for resizing you would do [imgView(32)] that will give the width or height depending on the V: which is vertical and the one without which is horizontal. Also for 100 , 400 put the V:|-(400)-[imgView] and for the 100 do |-(100)-[imgView]
|
1

If you're using autolayout like you said you should avoid to add subviews from code! If you want to do this you should programmatically add constrains also. Your code is fine but autolayout are messing with it. If you want dirty solution you should add this ImageView after everything is loaded (in viewDidAppear: method for example).

1 Comment

Thanks for your solution, but it didn't work, even in ViewDidAppear. What kind of constrains do I need to make it work? I don't think it's normal that I get these kind of NSLog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.