0

So i want to add subview (UIImageView) to my main view with the following code:

NSString *fileLocation = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"background_main.png"];
_aImage = [[UIImage alloc] initWithContentsOfFile:fileLocation];
_aImageView = [[UIImageView alloc] initWithImage:_aImage];
_aImageView.frame = CGRectMake(-200, 0, _aImage.size.width, _aImage.size.height);
[self.view addSubview:_aImageView];

as you can see - i'm adding UIImageView to position (-200;0);

But it shows up at default position : (0;0).

My view hierarchy:

  1. self.view -main view where i'm adding subview(UIImageView);
  2. _aImageView - UIImageView that i'm adding.

Results:

NSLog(@"image is %@",_aImageView); //result (-200 0; 420 568)

CGPoint test = [_aImageView convertPoint:_aImageView.frame.origin toView:self.view];
NSLog(@"convert %f %f",test.x,test.y); //result  (-400.000000; 0.000000)

I know that position is still (0;0) because i can see it when i testing my app. Later when i'm changing position of _aImageView to (-50;0) it's shows black screen. I know i'm missing something very important but i can't figure out why it shows up at (0;0) position.

Links to similar questions that i have checked:

Community
  • 1
  • 1
Vlad Z.
  • 3,401
  • 3
  • 32
  • 60
  • Position {0, 0} in which view. Give some details of your view hierarchy and how you're testing the resulting values. – Wain Oct 21 '13 at 17:25

2 Answers2

0

I believe you can't change the frame until in layoutSubviews. You could set the frame in layoutSubviews...

-(void)layoutSubviews
{
    [_aImageView setFrame:CGRectMake(-200, 0, _aImage.size.width, _aImage.size.height)];
}

Or set the frame first then add the image wherever you're initializing the UIImageView...

_aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-200, 0, _aImage.size.width, _aImage.size.height];
[_aImageView setImage:_aImage];
Hypnotiks
  • 9
  • 1
0

what worked for me in the past in this situations is doing something like this :

NSString *fileLocation = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"background_main.png"];
_aImage = [[UIImage alloc] initWithContentsOfFile:fileLocation];
_aImageView = [[UIImageView alloc] initWithImage:CGRectMake(-200, 0, _aImage.size.width, _aImage.size.height)];
[_aImageView setImage:_aImage];
[self.view addSubview:_aImageView];

If this doesn't work try changing it's frame in viewDidAppear method (i assume you're adding it in viewDidLoad).

Last solution is to set the view's center after you add it on the screen.

CGPoint pt=CGPointMake(-200+_aImageView.frame.size.width/2,_aImageView.frame.size.height/2);
_aImageView.center=pt;

If that fails the view is changed in another part of your code or the view is added on the screen properly but the image is nil.

skytz
  • 2,201
  • 2
  • 18
  • 23
  • interesting thing that is when i'm using init code i'm setting frame and image. With setting image everything is ok, but it doesn't matter what frame i'm setting , it's always default size and position. – Vlad Z. Oct 21 '13 at 22:19
  • your post was pretty useful though! – Vlad Z. Oct 21 '13 at 23:40