views:

184

answers:

2

I have created a UIView as a subview and to that subview I have added a UIImageView as a subview.

UIView *viewCreated;
UIButton *buttonCreated;
UIImageView *imageViewCreated;
CGRect myFrame = CGRectMake(0, 0, 1024, 1024);
viewCreated = [[UIView alloc] initWithFrame:myFrame];
[viewCreated setTag:intTag];
viewCreated.backgroundColor = [UIColor redColor];
[self.view addSubview:viewCreated];
[self randomize];
UIImage *d1Image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.png", randomNumber]];
imageViewCreated = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f, 1024.0f)];
[imageViewCreated setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.png", randomNumber]]]; 
[viewCreated addSubview:imageViewCreated];
//[imageViewCreated release];
return [viewCreated autorelease];

But when this code executes only the first subview is animated and resized. The UiimageView moves 200 pixels to the left but does not get resized.

NSLog(@"sender tag %i",[sender tag]);   
UIView *currentView = [self.view viewWithTag:[sender tag]];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];  
currentView.frame = CGRectMake(-200, 0,40, 102);
[UIView commitAnimations];

I am pretty sure am I not creating the subviews correctly programmatically, because when I do it in Interface Builder it works as expected.

Do I have to do something specific to actually attach the behavior of the second subview to the first subview?

A: 

A few suggestions:

  • Play with the contentMode property of the UIImageView. The default mode is UIViewContentModeScaleToFill. Something like UIViewContentModeScaleAspectFit may work better.

  • Make sure somewhere in your code, you aren't accidentally setting the autoresizesSubviews property to NO.

  • Instead of using frame sizing to scale the view, consider using the transform property and the CGAffineTransformMakeScale function. To make it move, you can also use CGAffineTransformMakeTranslation.

Ramin
Thanks for the reply. It got me on the right track.imageViewCreated.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
AaronG
A: 

I ended up using this to resolve my problem. Worked perfectly.

imageViewCreated.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
AaronG